| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{regsub}} | 
|  | 2 |  | 
|  | 3 | \stmodindex{regsub} | 
|  | 4 | This module defines a number of functions useful for working with | 
|  | 5 | regular expressions (see built-in module \code{regex}). | 
|  | 6 |  | 
| Guido van Rossum | 6076ea5 | 1996-06-26 19:24:22 +0000 | [diff] [blame] | 7 | Warning: these functions are not thread-safe. | 
|  | 8 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 9 | \renewcommand{\indexsubitem}{(in module regsub)} | 
| Guido van Rossum | 0b3f951 | 1996-08-09 21:43:21 +0000 | [diff] [blame^] | 10 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 11 | \begin{funcdesc}{sub}{pat\, repl\, str} | 
|  | 12 | Replace the first occurrence of pattern \var{pat} in string | 
|  | 13 | \var{str} by replacement \var{repl}.  If the pattern isn't found, | 
|  | 14 | the string is returned unchanged.  The pattern may be a string or an | 
|  | 15 | already 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} | 
|  | 20 | Replace all (non-overlapping) occurrences of pattern \var{pat} in | 
|  | 21 | string \var{str} by replacement \var{repl}.  The same rules as for | 
|  | 22 | \code{sub()} apply.  Empty matches for the pattern are replaced only | 
|  | 23 | when not adjacent to a previous match, so e.g. | 
|  | 24 | \code{gsub('', '-', 'abc')} returns \code{'-a-b-c-'}. | 
|  | 25 | \end{funcdesc} | 
|  | 26 |  | 
| Guido van Rossum | 0b3f951 | 1996-08-09 21:43:21 +0000 | [diff] [blame^] | 27 | \begin{funcdesc}{split}{str\, pat\optional{\, maxsplit}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 28 | Split the string \var{str} in fields separated by delimiters matching | 
|  | 29 | the pattern \var{pat}, and return a list containing the fields.  Only | 
|  | 30 | non-empty matches for the pattern are considered, so e.g. | 
|  | 31 | \code{split('a:b', ':*')} returns \code{['a', 'b']} and | 
| Guido van Rossum | 0b3f951 | 1996-08-09 21:43:21 +0000 | [diff] [blame^] | 32 | \code{split('abc', '')} returns \code{['abc']}.  The \var{maxsplit} | 
|  | 33 | defaults to 0. If it is nonzero, only \var{maxsplit} number of splits | 
|  | 34 | occur, and the remainder of the string is returned as the final | 
|  | 35 | element of the list. | 
|  | 36 | \end{funcdesc} | 
|  | 37 |  | 
|  | 38 | \begin{funcdesc}{splitx}{str\, pat\optional{\, maxsplit}} | 
|  | 39 | Split the string \var{str} in fields separated by delimiters matching | 
|  | 40 | the pattern \var{pat}, and return a list containing the fields as well | 
|  | 41 | as the separators.  For example, \code{splitx('a:::b', ':*')} returns | 
|  | 42 | \code{['a', ':::', 'b']}.  Otherwise, this function behaves the same | 
|  | 43 | as \code{split}. | 
|  | 44 | \end{funcdesc} | 
|  | 45 |  | 
|  | 46 | \begin{funcdesc}{capwords}{s\optional{\, pat}} | 
|  | 47 | Capitalize words separated by optional pattern \var{pat}.  The default | 
|  | 48 | pattern uses any characters except letters, digits and underscores as | 
|  | 49 | word delimiters.  Capitalization is done by changing the first | 
|  | 50 | character of each word to upper case. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 51 | \end{funcdesc} |