The re
module is the standard regular
expression-matching interface. Regular expression (RE) patterns are
specified as strings. This module must be imported.
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 the re
module:
A
or
ASCII
or (?a)
Makes w
,
W
, ,
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, the re.U
flag still exists (as well
as its synonym re.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
or
IGNORECASE
or (?i)
Case-insensitive matching.
L
or
LOCALE
or (?L)
Makes w
,
W
, ,
B
, s
, S
, d
, and D
dependent on the current
locale (default is Unicode for Python 3).
M
or
MULTILINE
or (?m)
Matches to each newline, not whole string.
S
or
DOTALL
or (?s)
.
matches
all characters, including
newline.
U
or
UNICODE
or (?u)
Makes w
,
W
, ,
B
, s
, S
, d
, and D
dependent on Unicode
character properties (new in version 2.0, and superfluous
in Python 3).
X
or
VERBOSE
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 pattern
string, returns a
corresponding MatchObject
instance, or None
if no
match. flags
as in compile
.
search(pattern, string [,
flags])
Scans through string
for a location matching pattern
; returns a corresponding
MatchObject
instance, or
None
if no match. flags
as in compile
.
split(pattern, string [,
maxsplit=0])
Splits string
by
occurrences of pattern
. If
capturing ()
are used in
pattern
, occurrences of
patterns or subpatterns are also returned.
sub(pattern, repl, string [,
count=0])
Returns string obtained by replacing the (first count)
leftmost nonoverlapping occurrences of pattern
(a string or an RE object) in
string
by repl
. repl
can be a string or a function
called with a single MatchObject
argument, which must
return the replacement string. repl
can also include sequence escapes
1
, 2
, etc., to use substrings that match
groups, or