Pattern | Meaning |
---|---|
. |
Any character except newline (unless re.DOTALL ) |
^ |
Start of string (or start of line if re.MULTILINE ) |
$ |
End of string (or end of line if re.MULTILINE ) |
\A |
Start of string (ignores MULTILINE ) |
\Z |
End of string (ignores MULTILINE ) |
\b |
Word boundary |
\B |
Not a word boundary |
\\ |
Escape special character |
Pattern | Meaning |
---|---|
[abc] |
Any of a , b , or c |
[^abc] |
Any character except a , b , or c |
[a-z] |
Range: any lowercase letter |
\d |
Digit [0-9] |
\D |
Not a digit |
\w |
Word character [a-zA-Z0-9_] |
\W |
Not a word character |
\s |
Whitespace (space, tab, newline, etc.) |
\S |
Not whitespace |
Pattern | Meaning |
---|---|
* |
0 or more |
+ |
1 or more |
? |
0 or 1 (optional) |
{n} |
Exactly n |
{n,} |
At least n |
{n,m} |
Between n and m |
*? , +? , ?? , {n,m}? |
Non-greedy versions |
Pattern | Meaning |
---|---|
(abc) |
Capture group |
(?:abc) |
Non-capturing group |
(?P<name>abc) |
Named group |
(?P=name) |
Backreference by name |
\1 , \2 |
Backreference by group number |
(?=abc) |
Positive lookahead |
(?!abc) |
Negative lookahead |
(?<=abc) |
Positive lookbehind (fixed-width) |
(?<!abc) |
Negative lookbehind (fixed-width) |
Pattern | Meaning |
---|---|
`a | b` |
Flag | Inline | Meaning |
---|---|---|
re.IGNORECASE |
(?i) |
Case-insensitive |
re.MULTILINE |
(?m) |
^ and $ match line boundaries |
re.DOTALL |
(?s) |
. matches newlines |
re.VERBOSE |
(?x) |
Allow whitespace/comments in pattern |
re.ASCII |
(?a) |
\w , \d , \s match only ASCII |
re.LOCALE |
(?L) |
Deprecated, locale-dependent classes |
re.UNICODE |
(?u) |
Unicode matching (default in Python 3) |
Escape | Meaning |
---|---|
\n , \r , \t |
Newline, carriage return, tab |
\xhh |
Hex byte |
\uhhhh |
Unicode code point (16-bit) |
\Uhhhhhhhh |
Unicode code point (32-bit) |
\ooo |
Octal value |
Function | Use |
---|---|
re.match(pat, s) |
Match at start |
re.fullmatch(pat, s) |
Match whole string |
re.search(pat, s) |
First match anywhere |
re.findall(pat, s) |
Return all matches (list of strings/tuples) |
re.finditer(pat, s) |
Iterator of match objects |
re.sub(pat, repl, s) |
Replace matches |
re.split(pat, s) |
Split by regex |
re.compile(pat, flags) |
Precompile regex |