blob: 6ee873d008842fc56bcf060a875b20f77544ce5b [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Standard Module \sectcode{regsub}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-regsub}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003
4\stmodindex{regsub}
5This module defines a number of functions useful for working with
6regular expressions (see built-in module \code{regex}).
7
Guido van Rossum6076ea51996-06-26 19:24:22 +00008Warning: these functions are not thread-safe.
9
Guido van Rossum77796191997-12-30 04:54:47 +000010\strong{Obsolescence note:}
11This module is obsolete as of Python version 1.5; it is still being
12maintained because much existing code still uses it. All new code in
Fred Drake16f88451998-01-22 20:47:26 +000013need of regular expressions should use the new \module{re} module, which
Guido van Rossum77796191997-12-30 04:54:47 +000014supports the more powerful and regular Perl-style regular expressions.
15Existing code should be converted. The standard library module
Fred Drake16f88451998-01-22 20:47:26 +000016\module{reconvert} helps in converting \code{regex} style regular
17expressions to \module{re} style regular expressions. (For more
Guido van Rossum77796191997-12-30 04:54:47 +000018conversion help, see the URL
Fred Drake859c7971998-03-06 15:11:30 +000019\url{http://starship.skyport.net/crew/amk/howto/regex-to-re.html}.)
Guido van Rossum77796191997-12-30 04:54:47 +000020
Guido van Rossum0b3f9511996-08-09 21:43:21 +000021
Fred Drakecce10901998-03-17 06:33:25 +000022\begin{funcdesc}{sub}{pat, repl, str}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000023Replace the first occurrence of pattern \var{pat} in string
24\var{str} by replacement \var{repl}. If the pattern isn't found,
25the string is returned unchanged. The pattern may be a string or an
26already compiled pattern. The replacement may contain references
27\samp{\e \var{digit}} to subpatterns and escaped backslashes.
28\end{funcdesc}
29
Fred Drakecce10901998-03-17 06:33:25 +000030\begin{funcdesc}{gsub}{pat, repl, str}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031Replace all (non-overlapping) occurrences of pattern \var{pat} in
32string \var{str} by replacement \var{repl}. The same rules as for
33\code{sub()} apply. Empty matches for the pattern are replaced only
34when not adjacent to a previous match, so e.g.
35\code{gsub('', '-', 'abc')} returns \code{'-a-b-c-'}.
36\end{funcdesc}
37
Fred Drakecce10901998-03-17 06:33:25 +000038\begin{funcdesc}{split}{str, pat\optional{, maxsplit}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000039Split the string \var{str} in fields separated by delimiters matching
40the pattern \var{pat}, and return a list containing the fields. Only
41non-empty matches for the pattern are considered, so e.g.
42\code{split('a:b', ':*')} returns \code{['a', 'b']} and
Guido van Rossum0b3f9511996-08-09 21:43:21 +000043\code{split('abc', '')} returns \code{['abc']}. The \var{maxsplit}
44defaults to 0. If it is nonzero, only \var{maxsplit} number of splits
45occur, and the remainder of the string is returned as the final
46element of the list.
47\end{funcdesc}
48
Fred Drakecce10901998-03-17 06:33:25 +000049\begin{funcdesc}{splitx}{str, pat\optional{, maxsplit}}
Guido van Rossum0b3f9511996-08-09 21:43:21 +000050Split the string \var{str} in fields separated by delimiters matching
51the pattern \var{pat}, and return a list containing the fields as well
52as the separators. For example, \code{splitx('a:::b', ':*')} returns
53\code{['a', ':::', 'b']}. Otherwise, this function behaves the same
54as \code{split}.
55\end{funcdesc}
56
Fred Drakecce10901998-03-17 06:33:25 +000057\begin{funcdesc}{capwords}{s\optional{, pat}}
Guido van Rossum0b3f9511996-08-09 21:43:21 +000058Capitalize words separated by optional pattern \var{pat}. The default
59pattern uses any characters except letters, digits and underscores as
60word delimiters. Capitalization is done by changing the first
61character of each word to upper case.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000062\end{funcdesc}
Barry Warsaw736bb061997-02-18 18:59:37 +000063
64\begin{funcdesc}{clear_cache}{}
65The regsub module maintains a cache of compiled regular expressions,
66keyed on the regular expression string and the syntax of the regex
67module at the time the expression was compiled. This function clears
68that cache.
69\end{funcdesc}