The website http://regexpal.com/ allows you to demo regular expressions on sample text. We will use this site in the steps below. Enter regular expressions in the top box and sample text in the bottom box.
b
, f
, or m
to your name (or the suffix of your name). (For example, I would want to match badrian
, fadrian
, and madrian
.)bananaFana()
, change the call to isVowel()
to instead use isVowelRegEx()
, and then run the program again. What's going on?\w
does? What do you think the +
sign does?
\s
does?
b
, f
, or m
.$
does?
\d
match? How can you match runs of multiple such things?Now download act_regex2.py. Download and save poem.txt in the same directory as act_regex2.py. Open poem.txt in your text editor, so you can refer back to it as the program operates on it.
printFirstRegexMatch()
is a string representing a regular expression. The r
before the open-quotation-mark indicates that the literal is a raw string literal, meaning that backslashes don't do anything special. This way, we can put backslashes into our regular expressions without needing to escape them.printFirstRegexMatch()
to be each of the following regular expressions:
r'\sg\w+'
r'\s[gG]\w+'
r'out'
r'\sout'
r'\w+out'
r'[\s\w]out'
r'[\s\w][Oo]ut'
Download act_regex3.py and put it in the same directory as everything else. This program works similarly to the last one, but it does something slightly different.
Read through the source code and change the regular expression that's being used. (Replace it with some of the exampls from the previous task.) Again, ask questions if you don't quite get what's going on.
Once you've figured out how the program works, it's time to design your own regular expressions. Change the regular expression so that the program does the following:
it
.it
. A word should be surrounded by whitespace; it's okay in this case to include the whitespace in your matches. There are five instances of the word it
.it
followed by at least one other letter. There are six such words. Can you make a pattern that will match the whole word but not include the whitespace on the ends?ing
. There are two such words.She'd
and I'll
), but write the expression to return any contraction.