In the previous tutorial in this series, you covered a lot of ground. You saw how to use re.search() to perform pattern matching with regexes in Python and learned about the many regex metacharacters and parsing flags that you can use to fine-tune your pattern-matching capabilities.
But as great as all that is, the re module has much more to offer.
In this tutorial, you’ll:
- Explore more functions, beyond
re.search(), that theremodule provides - Learn when and how to precompile a regex in Python into a regular expression object
- Discover useful things that you can do with the match object returned by the functions in the
remodule
Ready? Let’s dig in!
Get Your Code: Click here to download the free sample code that shows you how to use the functions and objects in Python’s re module.
re Module Functions
In addition to re.search(), the re module contains several other functions to help you perform regex-related tasks.
Note: You saw in the previous tutorial that re.search() can take an optional <flags> argument, which specifies flags that modify parsing behavior. All the functions shown below, with the exception of re.escape(), support the <flags> argument in the same way.
You can specify <flags> as either a positional argument or a keyword argument:
re.search(<regex>, <string>, <flags>) re.search(<regex>, <string>, flags=<flags>) The default for <flags> is always 0, which indicates no special modification of matching behavior. Remember from the discussion of flags in the previous tutorial that the re.UNICODE flag is always set by default.
The available regex functions in the Python re module fall into the following three categories:
- Searching functions
- Substitution functions
- Utility functions
The following sections explain these functions in more detail.
Searching Functions
Searching functions scan a search string for one or more matches of the specified regex:
| Function | Description |
|---|---|
re.search() | Scans a string for a regex match |
re.match() | Looks for a regex match at the beginning of a string |
re.fullmatch() | Looks for a regex match on an entire string |
re.findall() | Returns a list of all regex matches in a string |
re.finditer() | Returns an iterator that yields regex matches from a string |
As you can see from the table, these functions are similar to one another. But each one tweaks the searching functionality in its own way.
re.search(<regex>, <string>, flags=0)
Scans a string for a regex match.
If you worked through the previous tutorial in this series, then you should be well familiar with this function by now. re.search(<regex>, <string>) looks for any location in <string> where <regex> matches:
>>> re.search(r"(\d+)", "foo123bar") <re.Match object; span=(3, 6), match='123'> >>> re.search(r"[a-z]+", "123FOO456", flags=re.IGNORECASE) <re.Match object; span=(3, 6), match='FOO'> >>> print(re.search(r"\d+", "foo.bar")) None The function returns a match object if it finds a match and None otherwise.
re.match(<regex>, <string>, flags=0)
Looks for a regex match at the beginning of a string.
This is identical to re.search(), except that re.search() returns a match if <regex> matches anywhere in <string>, whereas re.match() returns a match only if <regex> matches at the beginning of <string>:
>>> re.search(r"\d+", "123foobar") <re.Match object; span=(0, 3), match='123'> >>> re.search(r"\d+", "foo123bar") <re.Match object; span=(3, 6), match='123'> >>> re.match(r"\d+", "123foobar") <re.Match object; span=(0, 3), match='123'> >>> print(re.match(r"\d+", "foo123bar")) None Read the full article at https://realpython.com/regex-python-part-2/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]


