Tip:
Highlight text to annotate it
X
Now that we've mastered single character regular expressions,
let's look into gluing them together.
We're going to need to find important bits of punctuation like slash greater than
or equals equals, to reason about JavaScript and HTML and thus write our web browser.
Thus we really need the ability to concatenate or put right next to each other
in repeat regular expressions.
Well, with regular expressions that's actually as simple as just writing,
two regular expressions right next to each other.
This matches the string a1, a2, b1, b2, c1, and c2--six strings in all.
In each one, the first letter comes in the first regular expression,
and the second letters, 1 or 2, matches the second part of the regular expression.
We've concatenated a through c and 1 through 2 together to match more complicated strings.
You may have noticed that we suddenly had quite a few strings
from a relatively small regular expression.
In fact, if we 0 through 9 next to 0 through 9,
there are a huge number of strings that we would match--100 in total.
Just as this matches 10 and this matches 10 when you put them together,
you match 10-squared strings.
So let's look for a two-digit number in the string July 28, 1921.
Here is a two-digit number, here is a two-digit number, and here is another one.
We'll end up getting 28, 18, and 21.
Now I'm looking for two-digit numbers in 12345--12 is a two-digit number,
34 is a two-digit number, but 5 actually does not qualify.
This regular expression requires that both subparts be matched.
July 28, 1821 is a good day for Peruvian Independence.
Let's brush up on matching these compound regular expressions.
Down here I've written a Python fragment
or Python program involving re.findall.
We're looking for a through z followed by 0 through 9 in this artificially constructed string
designed to be a bit tricky--a1 space 2b space cc3 space 44d.
I'm going to write out a bunch of possible answers, and I want you to tell me--
check all that apply which of the following are elements of the return value of this expression.
Here I've written nine possibilities.
Check each one that's a member of the list that's return by this Python expression.