Regular expressions allow you to perform pattern matching within strings.
Regular expressions serve as extremely useful building blocks when
you're looking to extract information from HTML content.
This demo will be a quick introduction to using regular
expressions for pattern matching in Python.
The RE module for regular expressions is available as a
part of your Python distribution.
Go ahead and import re.
In order to quickly get started with regular expressions,
we'll work with this very simple multiline string.
The RE module offers the match function to perform pattern
matching at the beginning of your string.
Re.match will match whatever pattern that you specified.
Here I specified the string Python only at the beginning of the string though.
Here we're looking to see whether the string contained in the
text variable starts with the word Python.
Let's print the output that we get out to screen.
And you can see that, yes, indeed, it does.
We get a match object as the return value.
The pattern or string Python was matched at the character positions 0 through 6.
The start and end functions on the match object will give you the starting
position and the ending position of the match found.
The pattern matches may not be a complete word.
I'm going to perform a match on pyt on the text string,
and you can see a match was found here as well spanning characters 0 through 3.
If you want your pattern to match just complete words
at the beginning of your string, you'll need to use the full match function.
Here full match will return the output None because pyt
did not match a word in its entirety.
Let's invoke the re.match function once again.
This time, we'll specify the string programming.
Now programming is not present at the beginning of a string,
it's present somewhere in the middle.
The match function matches patterns only at the beginning of the string,
which is why the output is None If you want to search for the
specified pattern across the entire string,
you'll need to use the search function on RE.
Here I search for the string programming within my text variable,
and there is a match at characters 43 through 54 just to note
that the end character index here is exclusive.
The search function can perform a match on the pattern
at any position within the string.
Here I search for Python, and a match object is returned.
Python was present at the beginning of the string and that is matched as well.
Now the word Python is present multiple times for the null string,
but the search function returned just the first instance that matched.
If you want to match all instances that are present,
you'll need to use the find all function.
The output here is a list of pattern matches.
The word Python was present twice in our string.
Both of the matches have been returned.
If you want to iterate over all of the pattern matches and get additional
information such as the character range of the matches,
you'll use the finditer function.
The finditer returns an iterator object which you can then iterate over.
This iterator function allows us to iterate over match objects.
Let's run a for loop over this output iterator,
and we'll print out the group of the match and the
starting and the ending character positions.
Group here refers to the term that was matched using our search pattern.
Observe that Python is present in the character
ranges 0 through 6 and 82 through 88.
The RE module allows you to use regular expressions
to tokenize your strings as well.
Here is a simple text string, "Python is simple, intuitive and fun to learn!
" I'm going to use the re.split function to
tokenize this input string on spaces.
And the output here is a list of tokens split on the space character.
These are all the words on my input text.
I can also use regular expressions to substitute a
matched pattern with another string.
Here I want to substitute the string Python with the
string The Python programming language.
The single occurrence of the word Python in the input text has been
replaced with this string The Python programming language.