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

Regular Expression Quantifiers

This document summarizes regular expression quantifiers, special characters, flags, character classes, assertions, groups, and methods in Python for matching, searching, splitting, and replacing strings using regular expressions. It explains common quantifiers like *, +, ?, {m,n} that specify how many times a subpattern can be repeated. It also covers special characters like \n, \r, \t, character escapes, and flags that affect matching.

Uploaded by

mistic93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views2 pages

Regular Expression Quantifiers

This document summarizes regular expression quantifiers, special characters, flags, character classes, assertions, groups, and methods in Python for matching, searching, splitting, and replacing strings using regular expressions. It explains common quantifiers like *, +, ?, {m,n} that specify how many times a subpattern can be repeated. It also covers special characters like \n, \r, \t, character escapes, and flags that affect matching.

Uploaded by

mistic93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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