This module defines a number of functions useful for working with
regular expressions (see built-in module regex
).
Warning: these functions are not thread-safe.
Obsolescence note:
This module is obsolete as of Python version 1.5; it is still being
maintained because much existing code still uses it. All new code in
need of regular expressions should use the new re
module, which
supports the more powerful and regular Perl-style regular expressions.
Existing code should be converted. The standard library module
reconvert
helps in converting regex
style regular
expressions to re
style regular expressions. (For more
conversion help, see the URL
`http://starship.skyport.net/crew/amk/regex/regex-to-re.html
'.)
- sub (pat, repl, str)
-
Replace the first occurrence of pattern pat in string
str by replacement repl. If the pattern isn't found,
the string is returned unchanged. The pattern may be a string or an
already compiled pattern. The replacement may contain references
`\digit' to subpatterns and escaped backslashes.
- gsub (pat, repl, str)
-
Replace all (non-overlapping) occurrences of pattern pat in
string str by replacement repl. The same rules as for
sub()
apply. Empty matches for the pattern are replaced only
when not adjacent to a previous match, so e.g.
gsub('', '-', 'abc')
returns '-a-b-c-'
.
- split (str, pat[, maxsplit])
-
Split the string str in fields separated by delimiters matching
the pattern pat, and return a list containing the fields. Only
non-empty matches for the pattern are considered, so e.g.
split('a:b', ':*')
returns ['a', 'b']
and
split('abc', '')
returns ['abc']
. The maxsplit
defaults to 0. If it is nonzero, only maxsplit number of splits
occur, and the remainder of the string is returned as the final
element of the list.
- splitx (str, pat[, maxsplit])
-
Split the string str in fields separated by delimiters matching
the pattern pat, and return a list containing the fields as well
as the separators. For example,
splitx('a:::b', ':*')
returns
['a', ':::', 'b']
. Otherwise, this function behaves the same
as split
.
- capwords (s[, pat])
-
Capitalize words separated by optional pattern pat. The default
pattern uses any characters except letters, digits and underscores as
word delimiters. Capitalization is done by changing the first
character of each word to upper case.
- clear_cache ()
-
The regsub module maintains a cache of compiled regular expressions,
keyed on the regular expression string and the syntax of the regex
module at the time the expression was compiled. This function clears
that cache.
guido@CNRI.Reston.Va.US