- [Instructor] For this Self Check exercise we'd like you
to create your own regular expression and test it,
and specifically we wanna match a simple street address
consisting of a number with one or more digits
followed by two words with one or more characters each.
So something like 123 Main Street in this case.
So go ahead and pause the video and give that a shot,
and then come back to see the answer.
Okay, let's go ahead and give this a shot.
So first we have to import the regular expression module.
Here is the regular expression.
So we've got a digit character class
that's saying one or more digits.
So the plus quantifier says one or more
of the preceding sub expression,
and that sub expression is a character class.
Followed by a space, which is a literal character.
Then we have two words,
and in our case we solved it saying
that we specifically wanted a word starting
with an uppercase letter followed
by zero or more lowercase letters and then a space.
And then again another word with an uppercase letter
and zero or more lowercase letters.
So let's go ahead and define that regular expression,
and then we'll go ahead and test it
with the 123 Main Street string that we defined up above.
And of course this is a very primitive
regular expression for matching addresses,
because addresses can certainly take many other forms.
But here we're going to use the regular expression
we specified as a raw string.
We'll pass that in as the first argument.
The string we're trying to match against is 123 Main Street.
And when I go ahead and execute that, we have a match.
On the other hand, if we remove the number at the beginning
and try to use that same regular expression,
we get a no match result in that case
because it's missing a key part,
the required one or more digits followed by a space
in your original regular expression.
So again these can get much more complicated
than what I'm showing you here, but we're just introducing
some of the fundamental capabilities of regular expressions.