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 or IGNORECASE or (?i)

Case-insensitive matching.

L or LOCALE 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 or MULTILINE or (?m)

Matches to each new line, not whole string.

S or DOTALL or (?s)

“.” matches all characters, including newline.

U or UNICODE or (?u)

Makes \w, \W, \b, \B, \s, \S, \d, and \D dependent on Unicode character properties (new in 2.0).

X or VERBOSE or (?x)

Ignore 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 ...

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.