Complete guide to Python's re module patterns and constructs
Based on Python's re module documentation. This reference covers all regular expression constructs available in Python's built-in re module, including special characters, character classes, quantifiers, groups, lookahead/lookbehind assertions, and more.
Showing 48 of 48 constructs
Construct | Matches | Description | Action |
---|---|---|---|
. | Any character except newline | Matches any single character except newline (\n) | |
^ | Start of string | Matches the start of the string | |
$ | End of string | Matches the end of the string | |
* | 0 or more repetitions | Matches 0 or more repetitions of the preceding RE | |
+ | 1 or more repetitions | Matches 1 or more repetitions of the preceding RE | |
? | 0 or 1 repetition | Matches 0 or 1 repetitions of the preceding RE | |
\ | Escape character | Escapes special characters or signals a special sequence | |
| | Alternation | A|B matches either A or B |
Construct | Matches | Description | Action |
---|---|---|---|
[abc] | Any of a, b, or c | Matches any single character in the brackets | |
[^abc] | Any character except a, b, or c | Matches any single character not in the brackets | |
[a-z] | Any lowercase letter | Matches any character in the range from a to z | |
[0-9] | Any digit | Matches any digit from 0 to 9 |
Construct | Matches | Description | Action |
---|---|---|---|
\d | Any decimal digit | Equivalent to [0-9] | |
\D | Any non-digit character | Equivalent to [^0-9] | |
\s | Any whitespace character | Matches [ \t\n\r\f\v] | |
\S | Any non-whitespace character | Equivalent to [^ \t\n\r\f\v] | |
\w | Any word character | Equivalent to [a-zA-Z0-9_] | |
\W | Any non-word character | Equivalent to [^a-zA-Z0-9_] |
Construct | Matches | Description | Action |
---|---|---|---|
{n} | Exactly n repetitions | Matches exactly n copies of the preceding RE | |
{n,} | n or more repetitions | Matches n or more copies of the preceding RE | |
{n,m} | Between n and m repetitions | Matches from n to m copies of the preceding RE | |
*? | 0 or more (non-greedy) | Non-greedy version of * | |
+? | 1 or more (non-greedy) | Non-greedy version of + | |
?? | 0 or 1 (non-greedy) | Non-greedy version of ? |
Construct | Matches | Description | Action |
---|---|---|---|
\b | Word boundary | Matches empty string at word boundaries | |
\B | Non-word boundary | Matches empty string when not at word boundary | |
\A | Start of string | Matches only at the start of the string | |
\Z | End of string | Matches only at the end of the string |
Construct | Matches | Description | Action |
---|---|---|---|
(pattern) | Capturing group | Matches pattern and captures the match | |
(?:pattern) | Non-capturing group | Matches pattern but does not capture the match | |
(?P<name>pattern) | Named capturing group | Matches pattern and captures with given name | |
\1 | Backreference to group 1 | Matches the same text as previously matched by group 1 | |
(?P=name) | Named backreference | Matches the same text as previously matched by named group |
Construct | Matches | Description | Action |
---|---|---|---|
(?=pattern) | Positive lookahead | Matches if pattern matches next, but does not consume | |
(?!pattern) | Negative lookahead | Matches if pattern does not match next | |
(?<=pattern) | Positive lookbehind | Matches if pattern matches before current position | |
(?<!pattern) | Negative lookbehind | Matches if pattern does not match before current position |
Construct | Matches | Description | Action |
---|---|---|---|
(?i) | Case insensitive | Makes the pattern case insensitive (re.IGNORECASE) | |
(?m) | Multiline mode | Makes ^ and $ match line boundaries (re.MULTILINE) | |
(?s) | Dot matches all | Makes . match any character including newline (re.DOTALL) | |
(?x) | Verbose mode | Allows whitespace and comments in pattern (re.VERBOSE) |
Construct | Matches | Description | Action |
---|---|---|---|
\t | Tab character | Matches a tab character | |
\n | Newline character | Matches a newline character | |
\r | Carriage return | Matches a carriage return character | |
\f | Form feed | Matches a form feed character | |
\v | Vertical tab | Matches a vertical tab character | |
\ooo | Octal character | Character with octal value ooo | |
\xhh | Hex character | Character with hex value hh |