blob: b4d3862ccb3447e5f4141cdf5c2884f297c2f8c6 [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
Fred Drake19479911998-02-13 06:58:54 +000021\setindexsubitem{(in module regsub)}
Guido van Rossum0b3f9511996-08-09 21:43:21 +000022
Fred Drakecce10901998-03-17 06:33:25 +000023\begin{funcdesc}{sub}{pat, repl, str}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000024Replace the first occurrence of pattern \var{pat} in string
25\var{str} by replacement \var{repl}. If the pattern isn't found,
26the string is returned unchanged. The pattern may be a string or an
27already compiled pattern. The replacement may contain references
28\samp{\e \var{digit}} to subpatterns and escaped backslashes.
29\end{funcdesc}
30
Fred Drakecce10901998-03-17 06:33:25 +000031\begin{funcdesc}{gsub}{pat, repl, str}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032Replace all (non-overlapping) occurrences of pattern \var{pat} in
33string \var{str} by replacement \var{repl}. The same rules as for
34\code{sub()} apply. Empty matches for the pattern are replaced only
35when not adjacent to a previous match, so e.g.
36\code{gsub('', '-', 'abc')} returns \code{'-a-b-c-'}.
37\end{funcdesc}
38
Fred Drakecce10901998-03-17 06:33:25 +000039\begin{funcdesc}{split}{str, pat\optional{, maxsplit}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000040Split the string \var{str} in fields separated by delimiters matching
41the pattern \var{pat}, and return a list containing the fields. Only
42non-empty matches for the pattern are considered, so e.g.
43\code{split('a:b', ':*')} returns \code{['a', 'b']} and
Guido van Rossum0b3f9511996-08-09 21:43:21 +000044\code{split('abc', '')} returns \code{['abc']}. The \var{maxsplit}
45defaults to 0. If it is nonzero, only \var{maxsplit} number of splits
46occur, and the remainder of the string is returned as the final
47element of the list.
48\end{funcdesc}
49
Fred Drakecce10901998-03-17 06:33:25 +000050\begin{funcdesc}{splitx}{str, pat\optional{, maxsplit}}
Guido van Rossum0b3f9511996-08-09 21:43:21 +000051Split the string \var{str} in fields separated by delimiters matching
52the pattern \var{pat}, and return a list containing the fields as well
53as the separators. For example, \code{splitx('a:::b', ':*')} returns
54\code{['a', ':::', 'b']}. Otherwise, this function behaves the same
55as \code{split}.
56\end{funcdesc}
57
Fred Drakecce10901998-03-17 06:33:25 +000058\begin{funcdesc}{capwords}{s\optional{, pat}}
Guido van Rossum0b3f9511996-08-09 21:43:21 +000059Capitalize words separated by optional pattern \var{pat}. The default
60pattern uses any characters except letters, digits and underscores as
61word delimiters. Capitalization is done by changing the first
62character of each word to upper case.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000063\end{funcdesc}
Barry Warsaw736bb061997-02-18 18:59:37 +000064
65\begin{funcdesc}{clear_cache}{}
66The regsub module maintains a cache of compiled regular expressions,
67keyed on the regular expression string and the syntax of the regex
68module at the time the expression was compiled. This function clears
69that cache.
70\end{funcdesc}