Regular expressions in Python: module re

Python doesn’t have a literal syntax for regular expressions, as it exists in Perl, JavaScript and Ruby (e.g. /d+/). The re module functions accept a string representing the regular expression. It is recommended to prepend the string with r’…’ to indicate a raw string, avoiding conflicts between Python’s escape sequences (like \b which is the ASCII backspace character) and the regex metacharacters (where \b means word boundary). See an example of using the match function:

>>> import re
>>> re.match(r'\d{4}$', '1234')
<_sre.SRE_Match object at 0x101161d30>

functions × methods¶
If regular expressions are constant and will be used many times, it is worth building a regular expression object by invoking the re.compile function. The returned object has methods that correspond to functions of the same name. Example with the match method:

>>> milhar = re.compile(r'\d{4}$')
>>> milhar.match('1234')
<_sre.SRE_Match object at 0x101161d30>

Leave a Reply