re
module for working with regular expressions.cat
matches the string "cat"
..
: Matches any single character except newline.^
: Matches the start of a string.$
: Matches the end of a string.*
: Matches 0 or more repetitions of the preceding character.+
: Matches 1 or more repetitions of the preceding character.?
: Matches 0 or 1 repetition of the preceding character.{m,n}
: Matches between m
and n
repetitions of the preceding character.[]
: Matches any single character within the brackets.|
: Acts as an OR operator.()
: Groups patterns together.a.b
matches "aab"
, "acb"
, but not "ab"
.^abc
matches "abc"
at the start of a string.xyz$
matches "xyz"
at the end of a string.\d
: Matches any digit (0-9).\D
: Matches any non-digit.\w
: Matches any word character (a-z, A-Z, 0-9, _).\W
: Matches any non-word character.\s
: Matches any whitespace character (space, tab, newline).\S
: Matches any non-whitespace character.\b
: Matches a word boundary.\B
: Matches a non-word boundary.\d{3}
matches any 3 digits (e.g., "123"
).\w+
matches one or more word characters (e.g., "hello"
).re
Modulere.match()
None
.re.search()
None
.re.findall()
re.finditer()
re.sub()
re.split()
()
to create groups in a regex.(?P<name>...)
syntax.r"..."
) for regex patterns to avoid escaping backslashes.re.VERBOSE
) for complex regex patterns.