0% found this document useful (0 votes)
241 views2 pages

Python Regular Expressions Quick Reference

Regular expressions (regex) allow defining patterns to match character combinations in strings. They use metacharacters like \w for word characters, quantifiers like * for 0 or more matches, character classes like [a-z] and alternation | for "or" to match patterns flexibly. The re module in Python provides functions like re.compile() to create regex objects, and regex objects support methods like re.search() to find matches and re.sub() to replace them. Regex patterns can also define groups to extract matched substrings and comments to document the meaning of parts of a pattern.

Uploaded by

Saurav Lama
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
241 views2 pages

Python Regular Expressions Quick Reference

Regular expressions (regex) allow defining patterns to match character combinations in strings. They use metacharacters like \w for word characters, quantifiers like * for 0 or more matches, character classes like [a-z] and alternation | for "or" to match patterns flexibly. The re module in Python provides functions like re.compile() to create regex objects, and regex objects support methods like re.search() to find matches and re.sub() to replace them. Regex patterns can also define groups to extract matched substrings and comments to document the meaning of parts of a pattern.

Uploaded by

Saurav Lama
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

CHARACTER CLASSES GROUPS

Python Use To match character Use To define


Regular \w Word character. [0-9_a-zA-Z] and (exp) Indexed group
Expressions Unicode word characters (?P<name>exp) Named group
\W Non-word character (?:exp) Noncapturing group
\d Decimal digit and Unicode digits (?=exp) Zero-width positive
SINGLE CHARACTERS \D Not a decimal digit lookahead
Use To match any character \s White-space character [ (?!exp) Zero-width negative
[set] In that set \t\n\r\f\v] and Unicode spaces lookahead
[^set] Not in that set \S Non-white-space char (?<=exp) Zero-width positive
[a–z] In the a-z range lookbehind. exp is fixed
[^a–z] Not in the a-z range QUANTIFIERS width
(?<!exp) Zero-width negative
. Any except \n (new line) Greedy Lazy Matches
lookbehind. exp is fixed
\char Escaped special character * *? 0 or more times width
+ +? 1 or more times
CONTROL CHARACTERS ? ?? 0 or 1 time INLINE OPTIONS
Use To match Unicode {n} {n}? Exactly n times Option Effect on match
\t Horizontal tab \u0009 {n,} {n,}? At least n times i Case-insensitive
\v Vertical tab \u000B {n,m} {n,m}? From n to m times m Multiline mode
\b Backspace \u0008 L Locale specific
\e Escape \u001B ANCHORS u Unicode dependent
\r Carriage return \u000D Use To specify position s Single-line mode
\f Form feed \u000C ^ At start of string or line x Ignore white space
\n New line \u000A \A At start of string
\a Bell (alarm) \u0007 \Z At end of string
$ At end of string or line June 2016
NON-ASCII CODES \b On word boundary
\B Not on word boundary http://bit.ly/PyRegEx
Use To match character with
\octal First digit 0 followed by 2 octal
digits or 3 octal digits Template: Microsoft/MSDN .NET Regular Expressions
\x hex 2-digit hex character code Python Reference: re module documentation
\u hex 4-digit hex character code
Created by: Chandra Lingam, Cotton Cola Designs LLC
BACKREFERENCES REGULAR EXPRESSION OPERATIONS
Use To match Module: re
\n Indexed group Processing a match
(?P=name) Named group Pattern matching with Compiled objects
Use method To
To initialize with Use constructor expand Replace a match
ALTERNATION Pattern re.compile(pattern) group Retrieve value of a group by
Use To match + flags re.compile(pattern,flags) number or name
a |b Either a or b groups Retrieve all subgroups as a
(?(n) yes if group n is matched Finding and replacing matched patterns. Use tuple
yes | no) no if group n isn't matched compiled object methods for additional groupdict Retrieve dictionary of named
(?(name) yes if name is matched options and fine-tuning parameters groups and values
yes | no) no if name isn't matched Use method To start Find starting index position of
re.match Find match at start of string a group
SUBSTITUTION re.search Find the first match end Find ending index position of
Use To substitute re.findall Retrieve all matching strings a group
\g<n> Substring matched by group re.finditer Retrive all matches
number n re.sub Replace a matching string
\g<name> Substring matched by group re.split Split text based on match
name
Getting info about regular expression patterns
June 2016
COMMENTS Use compiled To get
object API http://bit.ly/PyRegEx
Use To
(?# comment) Add inline comment groupindex Dictionary of Group names
and group number Template: Microsoft/MSDN .NET Regular Expressions
# Add x-mode comment to
end groups Capturing Group Count
Python Reference: re module documentation
pattern Pattern for compiled object
Created by: Chandra Lingam, Cotton Cola Designs LLC

You might also like