regex string processing
I have seen several topics about processing strings with regex being featured. But I was not able to find a dedicated regex thread, So I decided to start one, if no one objects, as there are notable experts on the forum who regularly offer guidance and solutions.
In fact starting the thread is just a convenience to get my own programming needs met. Currently I am trying to implement some basic text formatting options namely; quotes, bullet,s tablature, sequenced-numbers prefix, upper to lower, lower to upper (case) - each of these to operate on selected multi-line text, either adding or removing said formatting. Well I thought these must be easy to achieve, so I earnestly began learning regex with sed and grep. However ... to cut a long story short, it is very difficult to construct a regex argument that accommodates all manner of exceptions.
Actually I achieved my goals in the most basic conditions. Such as properly ended sentences, with no special characters ... and even in this case I was able, after a few days, to find partly working solutions. So now for the finesse.
Here is the body of text which I am using to test my sed constructions on. The text is echoed -e to a file, and the operations are upon the text in that file. But piping would be preferred.
This text has instances of the targeted strings at word end and beginning and , bracketed, quoted and what not. The text is just a real example.
the function I have so far is this, and any pointers will be gratefully implemented.
Code: Select all
echo -e "$EDITS" | for i in \( \) \{ \[ \} \] \? \& \$ \# \" \! \? \* \+ \: \@ \| \' \_ \- \. \, \; \= \^ \~ \% \/; do sed "s/$i/\\$i/g"; done > "$track"/MO
export L=$(echo -e "$pattern" | grep -v '^$' | wc -l)
export cnt=1
while read line
do
if [[ $(echo -e "$line" | grep -v '^$') != "" ]]; then
case "$BSSID" in
tabs+) sed -i "s/$line$/^\t$line/" "$track"/MO;;
tabs-) sed -i "/$line/s/\t//" "$track"/MO;;
upper) NP=$(echo -e "$line" | tr a-z A-Z)
sed -i "s/$line/$NP/g" "$track"/MO;;
lower) NP=$(echo -e "$line" | tr A-Z a-z)
sed -i "s/$line/$NP/g" "$track"/MO;;
bullets+) sed -i -e "/$line$/s/$line$/• $line/" "$track"/MO;;
bullets-) NP=$(echo -e $line | sed -e "s/• //")
sed -i "s/$line/$NP/" "$track"/MO;;
numbers+) sed -i -e "/$line$/s/$line$/$cnt\) $line/" "$track"/MO;;
numbers-) NP=$(echo -e "$line" | sed -E "s/[0-99]\) //")
sed -i "s/$line/$NP/" "$track"/MO;;
quotes+) case "$SCALE" in
0)[ "$cnt" -eq 1 ] && sed -i -e "s/$line$/$IDENTITY$line/" "$track"/MO
[ "$cnt" -eq "$L" ] && sed -i -e "s/$line$/$line$DOCS/" "$track"/MO;;
1) sed -i -e "s/$line/$IDENTITY$line$DOCS/" "$track"/MO;;
esac;;
quotes-) case "$SCALE" in
0)[ "$cnt" -eq 1 ] && sed -i -e "s/^$IDENTITY//" "$track"/MO
[ "$cnt" -eq "$L" ] && sed -i -e "/$DOCS$/s/$DOCS//" "$track"/MO;;
1) sed -r -i -e "s/^[$IDENTITY].*[$DOCS]$/s/^$IDENTITY//" -e "/[$DOCS]$/s/$DOCS$//g" "$track"/MO;;
2) NP=$(echo -e "$line" | cut -c2- | rev | cut -c2- | rev); sed -i "s/$line/$NP/g" "$track"/MO;;
esac;;
esac
export cnt=$((cnt + 1))
fi
done <<< "$pattern"
MODIFIED=$(cat "$track"/MO)
text
Code: Select all
Lesson Title: Modal Verbs
Comment:
Icon: tick
Objective:
Materials:
Guided Practice (15 minutes):
Independent Practice (10 minutes):
Closure (5 minutes):
Notes:
24 Modal Auxiliary Verbs Table
Introduction:
Modal verbs are a type of auxiliary verb used to express various meanings such as
obligation
possibility
permission
ability
suggestion
certainty
expectation
In English, the most commonly used modal verbs are:
can
could
may
might
shall
should
will
would
must
bought to
could is past tense of can.
would is past tense of will.
should is past tense of shall.
might is past tense of may.
In this lesson, we'll review the different uses of modal verbs and how to form sentences using them.
Modal verbs are an important part of English grammar, and mastering their use can greatly
enhance your ability to communicate effectively in various situations.
By understanding the different uses of modal verbs and practicing their use in context,
you can become a more confident and skilled speaker of English.
Lesson: 1
Expressing Ability: Can and could are used to express ability in present and past tense respectively.
Example: I can swim. She could play the guitar when she was younger.
Be able to can be used instead of can or could to express ability.
Example: I am able to speak Spanish fluently.
Expressing Possibility: May and might are used to express possibility.
Example: It may rain later today. She might come to the party if she finishes work early.
Could is also used to express possibility in a less certain way.
Example: He could be at home or at work.
Expressing Obligation:
Must and have to are used to express obligation.
Example: I must finish this report by tomorrow. You have to wear a helmet when riding a motorcycle.
Should and ought to are used to express advice or strong suggestion.
Example: You should drink plenty of water every day. She ought to get more exercise.
Expressing Permission:
Can and may are used to express permission.
Example: Can I borrow your pen? May I leave the meeting early?
Could and might are used to ask for permission in a more indirect or polite way.
Example: Could I ask you a question? Might I suggest an alternative approach?
Expressing Certainty:
Must is used to express a strong sense of certainty or deduction.
Example: He must be at home because his car is in the driveway.
Should is used to express a high degree of certainty or expectation.
Example: The train should arrive at the station at 8:30.
MODALS FUNCTIONS EXAMPLES
Will asking possibility / suggestion
Will you go to school?
It will rain today.You will not keep late hours at night before the exam.
Would requesting
Would you give me a pen?
Shall asking / possibility
Shall I do the work?
I hope I shall complete the project within a week.
Should suggestion / seeking advice
You should walk a mile in the morning.
Should we go for a walk?
Can ability / possibility
The boy can speak English fluently.
We can hold a condolence meeting for his death this Sunday.
Could ability / requesting
He could do the sum.
Could you help me to solve the problem?
May possibility / permission / offering
He may come here today.
May I come in?
May I get you a cup of tea?
Might possibility / suggestion
His statement might be true.
You might just as well go. certainty/strong probability / prohibition
You must obey your teacher.
Man must die one day.
You must be tired after a long journey.
We must not waste our time.
Challenge / negative force/ interrogation
He dare not say so. (not dares’)
I dare you to prove that you’ve said so.
He dare not follow you.
Who dares to enter the room?
Interrogation
You need not (needn’t) come here.
Need prohibition / interrogation
You need not (needn’t) come here.
Need he go there?
Used to habitual action in the past My father used to teach me English.
Ought to obligation strong likelihood You ought to work hard.
The lawyer ought to be able to help you.
Lesson: 2
Can and could are used to express ability in present and past tense respectively.
Example: I can swim. She could play the guitar when she was younger.
Example: I am able to speak Spanish fluently.
May and might are used to express possibility.
Example: It may rain later today. She might come to the party if she finishes work early.
Example: He could be at home or at work.
Must and have to are used to express obligation.
Example: I must finish this report by tomorrow. You have to wear a helmet when riding a motorcycle.
Example: You should drink plenty of water every day. She ought to get more exercise.
Example: Can I borrow your pen? May I leave the meeting early?
Example: Could I ask you a question? Might I suggest an alternative approach?
Must is used to express a strong sense of certainty or deduction.
Example: He must be at home because his car is in the driveway.
Example: The train should arrive at the station at 8:30.
EXERCISE:
Now, let's practice using modal verbs in sentences. Fill in the blanks with the appropriate modal verb:
I ___________ finish this project by the end of the week. (must / could / should)
She ___________ be at work right now. (must / might / can)
You ___________ turn off your phone during the movie. (should / must / can)
___________ I borrow your car tonight? (can / may / could)
We ___________ play tennis tomorrow if the weather is good. (can / may / should)