The re Pattern-Matching Module
The re
module is the standard regular
expression-matching interface. Regular expression (RE) patterns are
specified as strings. This module must be imported.
Module Functions
compile(pattern [, flags])
Compile an RE
pattern
string into a regular expression object, for later matching.flags
(combinable by bitwise|
operator) include the following available at the top-level of there
module:A
orASCII
or(?a)
Makes
\w
,\W
,\b
,\B
,\s
, and\S
perform ASCII-only matching instead of full Unicode matching. This is only meaningful for Unicode patterns and is ignored for byte patterns. Note that for backward compatibility, there.U
flag still exists (as well as its synonymre.UNICODE
and its embedded counterpart,?u
), but these are redundant in Python 3.0 since matches are Unicode by default for strings (and Unicode matching isn’t allowed for bytes).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 locale (default is Unicode for Python 3).M
orMULTILINE
or(?m)
Matches to each newline, 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 version 2.0, and superfluous in Python 3).X
orVERBOSE
or(?x)
Ignores whitespace in the pattern, outside character sets.
match(pattern, string [, flags])
If zero or more characters at start of
string
match the ...
Get Python Pocket Reference, 4th 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.