The module defines the following functions and constants, and an exception:
The expression's behaviour can be modified by specifying a
flags value. Values can be any of the following variables,
combined using bitwise OR (the |
operator).
The sequence
prog = re.compile(pat) result = prog.match(str)
is equivalent to
result = re.match(pat, str)
but the version using compile() is more efficient when the expression will be used several times in a single program.
None
if no
position in the string matches the pattern; note that this is
different from finding a zero-length match at some point in the string.
None
if the string does not
match the pattern; note that this is different from a zero-length
match.
Note: If you want to locate a match anywhere in string, use search() instead.
= 0
])
>>> re.split('\W+', 'Words, words, words.') ['Words', 'words', 'words', ''] >>> re.split('(\W+)', 'Words, words, words.') ['Words', ', ', 'words', ', ', 'words', '.', ''] >>> re.split('\W+', 'Words, words, words.', 1) ['Words', 'words, words.']
This function combines and extends the functionality of the old regsub.split() and regsub.splitx().
>>> re.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):', ... r'static PyObject*\npy_\1(void)\n{', ... 'def myfunc():') 'static PyObject*\npy_myfunc(void)\n{'
If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string. For example:
>>> def dashrepl(matchobj): .... if matchobj.group(0) == '-': return ' ' .... else: return '-' >>> re.sub('-{1,2}', dashrepl, 'pro----gram-files') 'pro--gram files'
The pattern may be a string or an RE object; if you need to specify
regular expression flags, you must use a RE object, or use embedded
modifiers in a pattern; for example, "sub("(?i)b+", "x", "bbbb
BBBB")" returns 'x x'
.
The optional argument count is the maximum number of pattern
occurrences to be replaced; count must be a non-negative
integer. If omitted or zero, all occurrences will be replaced.
Empty matches for the pattern are replaced only when not adjacent to
a previous match, so "sub('x*', '-', 'abc')" returns
'-a-b-c-'
.
In addition to character escapes and backreferences as described above, "\g<name>" will use the substring matched by the group named "name", as defined by the (?P<name>...) syntax. "\g<number>" uses the corresponding group number; "\g<2>" is therefore equivalent to "\2", but isn't ambiguous in a replacement such as "\g<2>0". "\20" would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character "0". The backreference "\g<0>" substitutes in the entire substring matched by the RE.
(new_string, number_of_subs_made)
.
See About this document... for information on suggesting changes.