Python library that provides regular expression matching operations similar to those found in Perl.
re
is the Python built-in module to deal with regular-expressions. It offers an intuitive, high-level mechanism to match patterns on strings.
The main functions to use from this module are:
re.compile
- this function takes a pattern and some possible flags and returns aPattern
object. This is mostly useful when using the same pattern in a loop - compile the pattern once before the loop, instead of at each iteration.re.match
- takes a pattern and a string (and possible flags) and tries to match the pattern from the beginning of the string. Returns aMatch
object.re.search
- similar tomatch
, but searches anywhere in the string.re.findall
- similar tosearch
, but returns a list with all matches found. The list contains strings rather thanMatch
objects. When the pattern contains groups, the list will consist of tuples containing the groups of each match.
The re
module also offers a regex-equivalent replacements for the built-in split
- re.split
- and replace
- re.sub
.