Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e74f8de

Browse files
committed
Added an example of a string value for the replacement parameter to
re.sub(). This closes SF bug #446136. Fixed description of RE modifiers so that RE{#} and RE{#,} are more clearly described and disambiguated (plain RE{#} had not been described at all). Reported by Jeremy Craven via email.
1 parent 630a63c commit e74f8de

1 file changed

Lines changed: 60 additions & 44 deletions

File tree

Doc/lib/libre.tex

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,19 @@ \subsection{Regular Expression Syntax \label{re-syntax}}
128128
\emph{few} characters as possible will be matched. Using \regexp{.*?}
129129
in the previous expression will match only \code{'<H1>'}.
130130

131+
\item[\code{\{\var{m}\}}]
132+
Specifies that exactly \var{m} copies of the previous RE should be
133+
matched; fewer matches cause the entire RE not to match. For example,
134+
\regexp{a\{6\}} will match exactly six \character{a} characters, but
135+
not five.
136+
131137
\item[\code{\{\var{m},\var{n}\}}] Causes the resulting RE to match from
132138
\var{m} to \var{n} repetitions of the preceding RE, attempting to
133139
match as many repetitions as possible. For example, \regexp{a\{3,5\}}
134140
will match from 3 to 5 \character{a} characters. Omitting \var{n}
135-
specifies an infinite upper bound; you can't omit \var{m}.
141+
specifies an infinite upper bound; you can't omit \var{m}. The comma
142+
may not be omitted or the modifier would be confused with the
143+
previously described form.
136144

137145
\item[\code{\{\var{m},\var{n}\}?}] Causes the resulting RE to
138146
match from \var{m} to \var{n} repetitions of the preceding RE,
@@ -497,21 +505,36 @@ \subsection{Module Contents}
497505
\end{funcdesc}
498506

499507
\begin{funcdesc}{findall}{pattern, string}
500-
Return a list of all non-overlapping matches of \var{pattern} in
501-
\var{string}. If one or more groups are present in the pattern,
502-
return a list of groups; this will be a list of tuples if the pattern
503-
has more than one group. Empty matches are included in the result.
504-
\versionadded{1.5.2}
508+
Return a list of all non-overlapping matches of \var{pattern} in
509+
\var{string}. If one or more groups are present in the pattern,
510+
return a list of groups; this will be a list of tuples if the
511+
pattern has more than one group. Empty matches are included in the
512+
result.
513+
\versionadded{1.5.2}
505514
\end{funcdesc}
506515

507-
\begin{funcdesc}{sub}{pattern, repl, string\optional{, count\code{ = 0}}}
508-
Return the string obtained by replacing the leftmost non-overlapping
509-
occurrences of \var{pattern} in \var{string} by the replacement
510-
\var{repl}. If the pattern isn't found, \var{string} is returned
511-
unchanged. \var{repl} can be a string or a function; if a function,
512-
it is called for every non-overlapping occurrence of \var{pattern}.
513-
The function takes a single match object argument, and returns the
514-
replacement string. For example:
516+
\begin{funcdesc}{sub}{pattern, repl, string\optional{, count}}
517+
Return the string obtained by replacing the leftmost non-overlapping
518+
occurrences of \var{pattern} in \var{string} by the replacement
519+
\var{repl}. If the pattern isn't found, \var{string} is returned
520+
unchanged. \var{repl} can be a string or a function; if it is a
521+
string, any backslash escapes in it are processed. That is,
522+
\samp{\e n} is converted to a single newline character, \samp{\e r}
523+
is converted to a linefeed, and so forth. Unknown escapes such as
524+
\samp{\e j} are left alone. Backreferences, such as \samp{\e6}, are
525+
replaced with the substring matched by group 6 in the pattern. For
526+
example:
527+
528+
\begin{verbatim}
529+
>>> re.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):',
530+
... r'static PyObject*\npy_\1(void)\n{',
531+
... 'def myfunc():')
532+
'static PyObject*\npy_myfunc(void)\n{'
533+
\end{verbatim}
534+
535+
If \var{repl} is a function, it is called for every non-overlapping
536+
occurrence of \var{pattern}. The function takes a single match
537+
object argument, and returns the replacement string. For example:
515538

516539
\begin{verbatim}
517540
>>> def dashrepl(matchobj):
@@ -521,38 +544,31 @@ \subsection{Module Contents}
521544
'pro--gram files'
522545
\end{verbatim}
523546

524-
The pattern may be a string or an RE object; if you need to specify
525-
regular expression flags, you must use a RE object, or use
526-
embedded modifiers in a pattern; for example,
527-
\samp{sub("(?i)b+", "x", "bbbb BBBB")} returns \code{'x x'}.
528-
529-
The optional argument \var{count} is the maximum number of pattern
530-
occurrences to be replaced; \var{count} must be a non-negative
531-
integer, and the default value of 0 means to replace all occurrences.
532-
533-
Empty matches for the pattern are replaced only when not adjacent to a
534-
previous match, so \samp{sub('x*', '-', 'abc')} returns
535-
\code{'-a-b-c-'}.
536-
537-
If \var{repl} is a string, any backslash escapes in it are processed.
538-
That is, \samp{\e n} is converted to a single newline character,
539-
\samp{\e r} is converted to a linefeed, and so forth. Unknown escapes
540-
such as \samp{\e j} are left alone. Backreferences, such as \samp{\e
541-
6}, are replaced with the substring matched by group 6 in the pattern.
542-
543-
In addition to character escapes and backreferences as described
544-
above, \samp{\e g<name>} will use the substring matched by the group
545-
named \samp{name}, as defined by the \regexp{(?P<name>...)} syntax.
546-
\samp{\e g<number>} uses the corresponding group number; \samp{\e
547-
g<2>} is therefore equivalent to \samp{\e 2}, but isn't ambiguous in a
548-
replacement such as \samp{\e g<2>0}. \samp{\e 20} would be
549-
interpreted as a reference to group 20, not a reference to group 2
550-
followed by the literal character \character{0}.
547+
The pattern may be a string or an RE object; if you need to specify
548+
regular expression flags, you must use a RE object, or use embedded
549+
modifiers in a pattern; for example, \samp{sub("(?i)b+", "x", "bbbb
550+
BBBB")} returns \code{'x x'}.
551+
552+
The optional argument \var{count} is the maximum number of pattern
553+
occurrences to be replaced; \var{count} must be a non-negative
554+
integer. If omitted or zero, all occurrences will be replaced.
555+
Empty matches for the pattern are replaced only when not adjacent to
556+
a previous match, so \samp{sub('x*', '-', 'abc')} returns
557+
\code{'-a-b-c-'}.
558+
559+
In addition to character escapes and backreferences as described
560+
above, \samp{\e g<name>} will use the substring matched by the group
561+
named \samp{name}, as defined by the \regexp{(?P<name>...)} syntax.
562+
\samp{\e g<number>} uses the corresponding group number;
563+
\samp{\e g<2>} is therefore equivalent to \samp{\e 2}, but isn't
564+
ambiguous in a replacement such as \samp{\e g<2>0}. \samp{\e 20}
565+
would be interpreted as a reference to group 20, not a reference to
566+
group 2 followed by the literal character \character{0}.
551567
\end{funcdesc}
552568

553-
\begin{funcdesc}{subn}{pattern, repl, string\optional{, count\code{ = 0}}}
554-
Perform the same operation as \function{sub()}, but return a tuple
555-
\code{(\var{new_string}, \var{number_of_subs_made})}.
569+
\begin{funcdesc}{subn}{pattern, repl, string\optional{, count}}
570+
Perform the same operation as \function{sub()}, but return a tuple
571+
\code{(\var{new_string}, \var{number_of_subs_made})}.
556572
\end{funcdesc}
557573

558574
\begin{funcdesc}{escape}{string}

0 commit comments

Comments
 (0)