blob: 4c1497cfe8907124a3cb41bea096553ff68ea73b [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Standard Module \sectcode{regsub}}
2
3\stmodindex{regsub}
4This module defines a number of functions useful for working with
5regular expressions (see built-in module \code{regex}).
6
Guido van Rossum6076ea51996-06-26 19:24:22 +00007Warning: these functions are not thread-safe.
8
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00009\renewcommand{\indexsubitem}{(in module regsub)}
Guido van Rossum0b3f9511996-08-09 21:43:21 +000010
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000011\begin{funcdesc}{sub}{pat\, repl\, str}
12Replace the first occurrence of pattern \var{pat} in string
13\var{str} by replacement \var{repl}. If the pattern isn't found,
14the string is returned unchanged. The pattern may be a string or an
15already compiled pattern. The replacement may contain references
16\samp{\e \var{digit}} to subpatterns and escaped backslashes.
17\end{funcdesc}
18
19\begin{funcdesc}{gsub}{pat\, repl\, str}
20Replace all (non-overlapping) occurrences of pattern \var{pat} in
21string \var{str} by replacement \var{repl}. The same rules as for
22\code{sub()} apply. Empty matches for the pattern are replaced only
23when not adjacent to a previous match, so e.g.
24\code{gsub('', '-', 'abc')} returns \code{'-a-b-c-'}.
25\end{funcdesc}
26
Guido van Rossum0b3f9511996-08-09 21:43:21 +000027\begin{funcdesc}{split}{str\, pat\optional{\, maxsplit}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000028Split the string \var{str} in fields separated by delimiters matching
29the pattern \var{pat}, and return a list containing the fields. Only
30non-empty matches for the pattern are considered, so e.g.
31\code{split('a:b', ':*')} returns \code{['a', 'b']} and
Guido van Rossum0b3f9511996-08-09 21:43:21 +000032\code{split('abc', '')} returns \code{['abc']}. The \var{maxsplit}
33defaults to 0. If it is nonzero, only \var{maxsplit} number of splits
34occur, and the remainder of the string is returned as the final
35element of the list.
36\end{funcdesc}
37
38\begin{funcdesc}{splitx}{str\, pat\optional{\, maxsplit}}
39Split the string \var{str} in fields separated by delimiters matching
40the pattern \var{pat}, and return a list containing the fields as well
41as the separators. For example, \code{splitx('a:::b', ':*')} returns
42\code{['a', ':::', 'b']}. Otherwise, this function behaves the same
43as \code{split}.
44\end{funcdesc}
45
46\begin{funcdesc}{capwords}{s\optional{\, pat}}
47Capitalize words separated by optional pattern \var{pat}. The default
48pattern uses any characters except letters, digits and underscores as
49word delimiters. Capitalization is done by changing the first
50character of each word to upper case.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000051\end{funcdesc}