blob: 6d489861d705271ea367236c139e66cd1e42bb4b [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 Rossum5fdeeea1994-01-02 01:22:07 +000010\renewcommand{\indexsubitem}{(in module regsub)}
Guido van Rossum0b3f9511996-08-09 21:43:21 +000011
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000012\begin{funcdesc}{sub}{pat\, repl\, str}
13Replace the first occurrence of pattern \var{pat} in string
14\var{str} by replacement \var{repl}. If the pattern isn't found,
15the string is returned unchanged. The pattern may be a string or an
16already compiled pattern. The replacement may contain references
17\samp{\e \var{digit}} to subpatterns and escaped backslashes.
18\end{funcdesc}
19
20\begin{funcdesc}{gsub}{pat\, repl\, str}
21Replace all (non-overlapping) occurrences of pattern \var{pat} in
22string \var{str} by replacement \var{repl}. The same rules as for
23\code{sub()} apply. Empty matches for the pattern are replaced only
24when not adjacent to a previous match, so e.g.
25\code{gsub('', '-', 'abc')} returns \code{'-a-b-c-'}.
26\end{funcdesc}
27
Guido van Rossum0b3f9511996-08-09 21:43:21 +000028\begin{funcdesc}{split}{str\, pat\optional{\, maxsplit}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000029Split the string \var{str} in fields separated by delimiters matching
30the pattern \var{pat}, and return a list containing the fields. Only
31non-empty matches for the pattern are considered, so e.g.
32\code{split('a:b', ':*')} returns \code{['a', 'b']} and
Guido van Rossum0b3f9511996-08-09 21:43:21 +000033\code{split('abc', '')} returns \code{['abc']}. The \var{maxsplit}
34defaults to 0. If it is nonzero, only \var{maxsplit} number of splits
35occur, and the remainder of the string is returned as the final
36element of the list.
37\end{funcdesc}
38
39\begin{funcdesc}{splitx}{str\, pat\optional{\, maxsplit}}
40Split the string \var{str} in fields separated by delimiters matching
41the pattern \var{pat}, and return a list containing the fields as well
42as the separators. For example, \code{splitx('a:::b', ':*')} returns
43\code{['a', ':::', 'b']}. Otherwise, this function behaves the same
44as \code{split}.
45\end{funcdesc}
46
47\begin{funcdesc}{capwords}{s\optional{\, pat}}
48Capitalize words separated by optional pattern \var{pat}. The default
49pattern uses any characters except letters, digits and underscores as
50word delimiters. Capitalization is done by changing the first
51character of each word to upper case.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052\end{funcdesc}
Barry Warsaw736bb061997-02-18 18:59:37 +000053
54\begin{funcdesc}{clear_cache}{}
55The regsub module maintains a cache of compiled regular expressions,
56keyed on the regular expression string and the syntax of the regex
57module at the time the expression was compiled. This function clears
58that cache.
59\end{funcdesc}