blob: bbb7671678a28b69b5a75e1bf50c4a5697746531 [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)}
10\begin{funcdesc}{sub}{pat\, repl\, str}
11Replace the first occurrence of pattern \var{pat} in string
12\var{str} by replacement \var{repl}. If the pattern isn't found,
13the string is returned unchanged. The pattern may be a string or an
14already compiled pattern. The replacement may contain references
15\samp{\e \var{digit}} to subpatterns and escaped backslashes.
16\end{funcdesc}
17
18\begin{funcdesc}{gsub}{pat\, repl\, str}
19Replace all (non-overlapping) occurrences of pattern \var{pat} in
20string \var{str} by replacement \var{repl}. The same rules as for
21\code{sub()} apply. Empty matches for the pattern are replaced only
22when not adjacent to a previous match, so e.g.
23\code{gsub('', '-', 'abc')} returns \code{'-a-b-c-'}.
24\end{funcdesc}
25
Guido van Rossum6076ea51996-06-26 19:24:22 +000026\begin{funcdesc}{split}{str\, pat\optional{\, retain}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000027Split the string \var{str} in fields separated by delimiters matching
28the pattern \var{pat}, and return a list containing the fields. Only
29non-empty matches for the pattern are considered, so e.g.
30\code{split('a:b', ':*')} returns \code{['a', 'b']} and
31\code{split('abc', '')} returns \code{['abc']}.
Guido van Rossum6076ea51996-06-26 19:24:22 +000032If the optional \var{retain} argument is true, the separators are also
33inserted in the list, so e.g. \code{split('a:::b', ':*', 1)} returns
34\code{['a', ':::', 'b']}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000035\end{funcdesc}