The re module handles regular expressions in Python
A regular expression can be used to find all matches in a string or simply test if a match exists.
Regular expressions help you to quickly collect some items from large piles of data just by defining some grammar rules.
Related course: Complete Python Bootcamp: Go from zero to hero in Python 3
Create a string to run the regular expression. As example phrase we’ll use the famous American tongue twister “wouldchuck”.
Find all matches for a string using a regular expression. Call the method findall() with as parameters the regular expression and the search space.
Try the program below:
string = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
matches = re.findall(r'woo\w*', string)
It returns the matches for the regular expression.
We define starting letters “woo”, followed by a word character \w that matches zero or more times * .
To test if the data a matching regular expression, use the search method. This returns true or false, that can be matched with an if statement:
if re.search(r'woo\w*', string):
Checkout this cheat sheet if you want to write your own regular expressions.
- Find out if regular expressions are case sensitive
- Create a regular expression for phone numbers