diff options
author | Barry Warsaw <barry@python.org> | 1997-11-20 00:15:13 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 1997-11-20 00:15:13 (GMT) |
commit | 4552f3d6def852342036774163b96e556d626619 (patch) | |
tree | de72eabcd9902b6dbee6399a4d044f5aed152aa7 | |
parent | bd1169a93e4c42156e899e2f80a1349e8006da25 (diff) | |
download | cpython-4552f3d6def852342036774163b96e556d626619.zip cpython-4552f3d6def852342036774163b96e556d626619.tar.gz cpython-4552f3d6def852342036774163b96e556d626619.tar.bz2 |
In the description of sub(), give a better explanation of the
interface when repl is a function. Also give a simple example of
using a function repl.
-rw-r--r-- | Doc/lib/libre.tex | 22 | ||||
-rw-r--r-- | Doc/libre.tex | 22 |
2 files changed, 36 insertions, 8 deletions
diff --git a/Doc/lib/libre.tex b/Doc/lib/libre.tex index bf6aeb8..0d14164 100644 --- a/Doc/lib/libre.tex +++ b/Doc/lib/libre.tex @@ -308,10 +308,24 @@ regular expression metacharacters in it. \begin{funcdesc}{sub}{pattern\, repl\, string\optional{, count=0}} Return the string obtained by replacing the leftmost non-overlapping occurrences of \var{pattern} in \var{string} by the replacement -\var{repl}, which can be a string or the function that returns a string. If the pattern isn't found, \var{string} is returned unchanged. The -pattern may be a string or a regexp object; if you need to specify -regular expression flags, you must use a regexp object, or use -embedded modifiers in a pattern string; e.g. +\var{repl}. If the pattern isn't found, \var{string} is returned +unchanged. \var{repl} can be a string or a function; if a function, +it is called for every non-overlapping occurance of \var{pattern}. +The function takes a single match object argument, and +returns the replacement string. For example: +% +\bcode\begin{verbatim} +>>> def dashrepl(matchobj): +... if matchobj.group(0) == '-': return ' ' +... else: return '-' +>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files') +'pro--gram files' +\end{verbatim}\ecode +% +The pattern may be a string or a +regexp object; if you need to specify regular expression flags, you +must use a regexp object, or use embedded modifiers in a pattern +string; e.g. % \bcode\begin{verbatim} sub("(?i)b+", "x", "bbbb BBBB") returns 'x x'. diff --git a/Doc/libre.tex b/Doc/libre.tex index bf6aeb8..0d14164 100644 --- a/Doc/libre.tex +++ b/Doc/libre.tex @@ -308,10 +308,24 @@ regular expression metacharacters in it. \begin{funcdesc}{sub}{pattern\, repl\, string\optional{, count=0}} Return the string obtained by replacing the leftmost non-overlapping occurrences of \var{pattern} in \var{string} by the replacement -\var{repl}, which can be a string or the function that returns a string. If the pattern isn't found, \var{string} is returned unchanged. The -pattern may be a string or a regexp object; if you need to specify -regular expression flags, you must use a regexp object, or use -embedded modifiers in a pattern string; e.g. +\var{repl}. If the pattern isn't found, \var{string} is returned +unchanged. \var{repl} can be a string or a function; if a function, +it is called for every non-overlapping occurance of \var{pattern}. +The function takes a single match object argument, and +returns the replacement string. For example: +% +\bcode\begin{verbatim} +>>> def dashrepl(matchobj): +... if matchobj.group(0) == '-': return ' ' +... else: return '-' +>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files') +'pro--gram files' +\end{verbatim}\ecode +% +The pattern may be a string or a +regexp object; if you need to specify regular expression flags, you +must use a regexp object, or use embedded modifiers in a pattern +string; e.g. % \bcode\begin{verbatim} sub("(?i)b+", "x", "bbbb BBBB") returns 'x x'. |