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
patternstring into a regular expression object, for later matching.flags(combinable by bitwise|operator) include the following available at the top-level of theremodule:AorASCIIor(?a)Makes
\w,\W,\b,\B,\s, and\Sperform 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.Uflag still exists (as well as its synonymre.UNICODEand 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).IorIGNORECASEor(?i)Case-insensitive matching.
LorLOCALEor(?L)Makes
\w,\W,\b,\B,\s,\S,\d, and\Ddependent on the current locale (default is Unicode for Python 3).MorMULTILINEor(?m)Matches to each newline, not whole string.
SorDOTALLor(?s).matches all characters, including newline.UorUNICODEor(?u)Makes
\w,\W,\b,\B,\s,\S,\d, and\Ddependent on Unicode character properties (new in version 2.0, and superfluous in Python 3).XorVERBOSEor(?x)Ignores whitespace in the pattern, outside character sets.
match(pattern, string [, flags])If zero or more characters at start of
stringmatch the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access