python-regex-cheatsheet
python-regex-cheatsheet
python-regex-cheatsheet
Expressions (?:...)
(?P<name>...)
Non-capturing version of regular parens
Create a named capturing group.
(?P=name) Match whatever matched prev named group
(?#...) A comment; ignored.
Non-special chars match themselves. Exceptions are (?=...) Lookahead assertion, match without consuming
special characters: (?!...) Negative lookahead assertion
(?<=...) Lookbehind assertion, match if preceded
(?<!...) Negative lookbehind assertion
\ Escape special char or start a sequence. (?(id)y|n) Match 'y' if group 'id' matched, else 'n'
. Match any char except newline, see re.DOTALL
^ Match start of the string, see re.MULTILINE
$ Match end of the string, see re.MULTILINE Flags for re.compile(), etc. Combine with '|':
[] Enclose a set of matchable chars
R|S Match either regex R or regex S. re.I == re.IGNORECASE Ignore case
() Create capture group, & indicate precedence re.L == re.LOCALE Make \w, \b, and \s locale dependent
re.M == re.MULTILINE Multiline
re.S == re.DOTALL Dot matches all (including newline)
re.U == re.UNICODE Make \w, \b, \d, and \s unicode dependent
After '[', enclose a set, the only special chars are: re.X == re.VERBOSE Verbose (unescaped whitespace in pattern
is ignored, and '#' marks comment lines)