π Edit page
β Add page
re
Using the
re
regex module in Python
Resources
- re in Python3 docs
Methods
# Search string starting from the left.
re.search
# Search at beginning of string.
re.match
# If the whole string matches the regular expression pattern
re.fullmatch
# Return list.
re.findall
Compile
Itβs more efficient to compile a pattern once if its going to be used.
PATTERN = re.compile(r'\w)
PATTERN.search(my_string)
Flags
IGNORECASE
MULTILINE
VERBOSE
DOTALL
DEBUG
e.g.
re.search(flags=re.IGNORECASE)
Matching groups
From docs.
Group
>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m.group(0) # The entire match
'Isaac Newton'
>>> m.group(1) # The first parenthesized subgroup.
'Isaac'
>>> m.group(2) # The second parenthesized subgroup.
'Newton'
>>> m.group(1, 2) # Multiple arguments give us a tuple.
('Isaac', 'Newton')
Groups
>>> m = re.match(r"(\d+)\.(\d+)", "24.1632")
>>> m.groups()
('24', '1632')
Make group optional.
>>> m = re.match(r"(\d+)\.?(\d+)?", "24")
>>> m.groups() # Second group defaults to None.
('24', None)
>>> m.groups('0') # Now, the second group defaults to '0'.
('24', '0')
Groupdict
>>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
>>> m.groupdict()
{'first_name': 'Malcolm', 'last_name': 'Reynolds'}
Slugs
Convert to slug
slug = re.sub(r'[\W_]+', '-', text.lower())
For more robust solution including handling special characters, see
- SO answers
python-slugify
Match a slug
r'^[a-z0-9]+(?:-[a-z0-9]+)*$'