The re Pattern-Matching Module
The re
module is the standard regular
expression-matching interface (new in 1.5). Regular expression (RE)
patterns are specified as strings. This module must be
imported.[11]
Module Functions
compile(pattern [, flags])
Compile an RE
pattern
string into a regular expression object, for later matching.flags
(combinable by bitwise | operator):I
orIGNORECASE
or(?i)
Case-insensitive matching.
L
orLOCALE
or(?L)
Makes
\w
,\W
,\b
,\B
,\s
,\S
,\d
, and\D
dependent on the current 8-bit locale (default is 7-bit U.S. ASCII).M
orMULTILINE
or(?m)
Matches to each new line, not whole string.
S
orDOTALL
or(?s)
“.” matches all characters, including newline.
U
orUNICODE
or(?u)
Makes
\w
,\W
,\b
,\B
,\s
,\S
,\d
, and\D
dependent on Unicode character properties (new in 2.0).X
orVERBOSE
or(?x)
Ignore whitespace in the pattern, outside character sets.
match(pattern, string [, flags])
If zero or more characters at start of
string
match thepattern
string, returns a corresponding MatchObject instance, orNone
if no match.flags
as incompile
.search(pattern, string [, flags])
Scans through
string
for a location matchingpattern
; returns a corresponding MatchObject instance, orNone
if no match.flags
as incompile
.split(pattern, string [, maxsplit=0])
Splits
string
by occurrences ofpattern
. If capturing( )
are used inpattern
, occurrences of patterns or subpatterns are also returned.sub(pattern, repl, string [, count=0])
Returns string obtained by replacing the (first ...
Get Python Pocket Reference, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.