Python Regex Guide
[Link](pattern, string)
Tries to match a pattern at the beginning of the string. Returns a match object if found, else None.
[Link](pattern, string)
Scans the entire string and returns the first match object if the pattern is found, else None.
[Link](pattern, string)
Returns all non-overlapping matches of the pattern in the string as a list of strings.
[Link](pattern, string)
Returns an iterator yielding match objects for all non-overlapping matches of the pattern.
[Link](pattern, string)
Checks if the entire string matches the pattern exactly. Returns a match object or None.
[Link](pattern, repl, string)
Replaces occurrences of the pattern in the string with repl. Returns the new string.
[Link](pattern, string)
Splits the string by the occurrences of the pattern. Returns a list.
[Link](pattern)
Compiles a regex pattern into a regex object for repeated use. More efficient for multiple operations.
[Link](string)
Escapes all special regex characters in the string so it can be used literally in a regex pattern.
Common Regex Tokens
.
Matches any character except newline.
^
Matches the start of the string.
$
Matches the end of the string.
*
Matches 0 or more repetitions.
+
Matches 1 or more repetitions.
?
Matches 0 or 1 repetition.
{n}
Matches exactly n repetitions.
{n,}
Matches n or more repetitions.
{n,m}
Matches between n and m repetitions.
[]
Defines a character class. Example: [a-z] matches any lowercase letter.
[^]
Negates a character class. Example: [^0-9] matches anything except digits.
\d
Matches any digit (0-9).
\D
Matches any non-digit.
\w
Matches any word character (letters, digits, underscore).
\W
Matches any non-word character.
\s
Matches any whitespace (spaces, tabs, newlines).
\S
Matches any non-whitespace character.
\b
Matches a word boundary (position between word char and non-word char).
\B
Matches a position that is NOT a word boundary.
|
Acts as OR. Example: (cat|dog) matches 'cat' or 'dog'.
()
Capturing group. Returns the matched text.
(?:)
Non-capturing group. Groups regex without capturing.