Regular Expression Quantifiers

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Regular Expression Quantifiers

*
0 or more
+
1 or more
?
0 or 1
{2}
Exactly 2
{2, 5}
Between 2 and 5
{2,}
2 or more
Regular Expression Special Characters
(,5}
Up to 5
\n
Newline
Default is greedy. Append ? for reluctant.
\r
Carriage return
\t
Tab
\YYY
Octal character YYY
\xYY
Hexadecimal
character
YY Replacement
Regular
Expression
\g<0>
Insert entire match
\g<Y>
Insert match Y (name or number)
\YExpression Basics
Insert group numbered Y
Regular

Regular expressions Python

.
a
ab
a|b
a*
i
\
m
s
x
L
[ab-d]
u
[^ab-d]
(?iLmsux)
[\b]
\d
\D
\s
\S
\w
\W

(...)
(?P<Y>...)
(?:...)
\Y
(?P=Y)
(?#...)

Any character except newline


The character a
The string ab
a or b
Regular Expression Flags
0 or more a's
Ignore case
Escapes a special character
^ and $ match start and end of line
. matches newline as well
Allow spaces
and comments
Regular
Expression
Character Classes
Locale
character
classes
One character of: a, b, c, d
Unicode
character
classes
One
character
except:
a, b, c, d
Set
flags
within
regex
Backspace character
One digit
One non-digit
One whitespace
One non-whitespace
One word character
One non-word character

Regular Expression Assertions


^
Start
of string
Regular Expression Groups
\A
Start of string, ignores m flag
Capturing group
$
Capturing group
named Y End of string
\Z
End of string, ignores m flag
Non-capturing group
\b captured groupWord boundary
Match the Y'th
\B
Match the named
group Y Non-word boundary
Positive lookahead
Comment (?=...)
(?!...)
Negative lookahead
(?<=...)
Positive lookbehind
(?<!...)
Negative lookbehind
(?()|)
Conditional

.match(string[, pos, endpos]) ->MatchObject


.search(string[, pos, endpos]) ->MatchObject
.findall(string[, pos, endpos]) ->list of strings
.finditer(string[, pos, endpos]) ->iter of MatchObjects
.split(string[, maxsplit]) ->list of strings
.sub(repl, string[, count]) ->string
.subn(repl, string[, count]) ->(string, int)
.flags # int, Passed to compile()
.groups # int, Number of capturing groups
.groupindex # {}, Maps group names to ints
.pattern # string, Passed to compile()

You might also like