So far we've performed just very simple string
matches using the RE module in Python.
Regular expressions allow us to specify very complex
pattern matches using special sequences,
and that's what we'll see here in this clip.
Here is the text that we'll work with.
It talks about Python and its different versions.
In order to use regular expressions with special sequences,
you need to compile this regular expression into a pattern using re.compile.
The \d special sequence here is a pattern that matches
any decimal digit from 0 through 9.
Re.compile gives us a regular expression pattern,
which can then be used within string to find pattern matches.
I'll now use this compile pattern to find matches within out text.
This pattern will find all decimals within our
string giving us a list of decimals.
This regular expression has allowed us to extract all of
the numbers present within our string.
Special sequences, as you can see, are very powerful.
Let's try this once again with the \d special sequence,
which will match any non-digit character.
This compile pattern will find all characters except decimal
digits which are present within our string.
Observe that the output includes all characters including
spaces and the period character but no decimals.
Let's take a look at another special sequence.
The \s will match any wide space character, whether it's a tab or a new line.
Let's find all wide space characters within our text.
And you can see, all of the wide spaces and the new lines have been extracted.
No alphabets or digits have been matched.
Instead of explicitly compiling a regular expression pattern,
you can simply specify that a particular string is a
regular expression using this r prefix.
This actually makes your regex strings far easier to maintain.
The \S will match any non-wide space character.
This will return alphabets, as well as digits, but no wide spaces.
The regular expression, \w, will match any alphanumeric character,
whether it's alphabets or digits, but it will not match any wide spaces.
The only special character that \w matches is the underscore,
no other special characters.
The \W will match any non-alphanumeric character.
This is the opposite of \w.
You can see the result.
All of the spaces, the periods, and the \n's were matched,
but none of the alphanumeric characters.
Regular expressions are incredibly powerful and vast.
It's impossible to cover all of the sequences that are possible
with regular expressions with a few clips.
Observe this regular expression here.
We've specified 0-9 within square brackets.
This will match all decimals from 0 through 9, both included.
The square brackets say match any of the patterns that are
specified within the square brackets.
This regular expression will match all of the numbers
that are present within our text.
Let's say you want to match just a few decimals within your string, not all.
You can specify the decimals that you're interested in within square brackets.
This will match all numbers from 5 through 9, no numbers below 5.
There's exactly one match in our string here, the number 7.
Here is another example using square brackets.
Match all numbers from 0 through 9 and all characters from
uppercase A all the way through to uppercase Z,
both inclusive.
The result will be matches of uppercase alphabets and decimals,
no lowercase characters have been matched.
Let's tweak our pattern a little bit.
I want to match those characters which are 0 through 9,
lowercase a to lowercase c, and uppercase A all the way through to uppercase P.
You can use re in specifications in the square brackets
to make your patterns very granular.
Regular expressions also support wild cards such as the asterisk character.
Find all strings which start with py and have something after them,
zero or more characters.
The word Python is present four times in my input string.
There are four matches for py.
By default, regular expression pattern matches of this kind are case sensitive.
This particular match will find nothing because P is
present in uppercase in our string,
whereas we have specified p in a lowercase here within the regular expression.
If you want your pattern matches to be case insensitive,
you'll need to compile your pattern using re.ignore case.
Let's find this case insensitive match in our input text and
the four instances of py are extracted.