[Python-Dev] Missing arguments in RE functions
Noam Raphael
noamr at myrealbox.com
Wed Sep 8 00:23:35 CEST 2004
Raymond Hettinger wrote:
>+1
>
>I've need both of these more than once.
>
>Are you up to crafting the code?
>
>
>
Thanks!
Are these diffs ok?
Noam
-------------- next part --------------
*** /home/noam/python/old/sre.py Tue Sep 7 23:36:36 2004
--- sre.py Tue Sep 7 23:40:53 2004
***************
*** 123,147 ****
# --------------------------------------------------------------------
# public interface
! def match(pattern, string, flags=0):
"""Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found."""
! return _compile(pattern, flags).match(string)
! def search(pattern, string, flags=0):
"""Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found."""
! return _compile(pattern, flags).search(string)
! def sub(pattern, repl, string, count=0):
"""Return the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the
replacement repl. repl can be either a string or a callable;
if a callable, it's passed the match object and must return
a replacement string to be used."""
! return _compile(pattern, 0).sub(repl, string, count)
! def subn(pattern, repl, string, count=0):
"""Return a 2-tuple containing (new_string, number).
new_string is the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in the source
--- 123,147 ----
# --------------------------------------------------------------------
# public interface
! def match(pattern, string, flags=0, pos=0, endpos=sys.maxint):
"""Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found."""
! return _compile(pattern, flags).match(string, pos, endpos)
! def search(pattern, string, flags=0, pos=0, endpos=sys.maxint):
"""Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found."""
! return _compile(pattern, flags).search(string, pos, endpos)
! def sub(pattern, repl, string, count=0, flags=0):
"""Return the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the
replacement repl. repl can be either a string or a callable;
if a callable, it's passed the match object and must return
a replacement string to be used."""
! return _compile(pattern, flags).sub(repl, string, count)
! def subn(pattern, repl, string, count=0, flags=0):
"""Return a 2-tuple containing (new_string, number).
new_string is the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in the source
***************
*** 149,162 ****
substitutions that were made. repl can be either a string or a
callable; if a callable, it's passed the match object and must
return a replacement string to be used."""
! return _compile(pattern, 0).subn(repl, string, count)
! def split(pattern, string, maxsplit=0):
"""Split the source string by the occurrences of the pattern,
returning a list containing the resulting substrings."""
! return _compile(pattern, 0).split(string, maxsplit)
! def findall(pattern, string):
"""Return a list of all non-overlapping matches in the string.
If one or more groups are present in the pattern, return a
--- 149,162 ----
substitutions that were made. repl can be either a string or a
callable; if a callable, it's passed the match object and must
return a replacement string to be used."""
! return _compile(pattern, flags).subn(repl, string, count)
! def split(pattern, string, maxsplit=0, flags=0):
"""Split the source string by the occurrences of the pattern,
returning a list containing the resulting substrings."""
! return _compile(pattern, flags).split(string, maxsplit)
! def findall(pattern, string, flags=0, pos=0, endpos=sys.maxint):
"""Return a list of all non-overlapping matches in the string.
If one or more groups are present in the pattern, return a
***************
*** 164,179 ****
has more than one group.
Empty matches are included in the result."""
! return _compile(pattern, 0).findall(string)
if sys.hexversion >= 0x02020000:
__all__.append("finditer")
! def finditer(pattern, string):
"""Return an iterator over all non-overlapping matches in the
string. For each match, the iterator returns a match object.
Empty matches are included in the result."""
! return _compile(pattern, 0).finditer(string)
def compile(pattern, flags=0):
"Compile a regular expression pattern, returning a pattern object."
--- 164,179 ----
has more than one group.
Empty matches are included in the result."""
! return _compile(pattern, flags).findall(string, pos, endpos)
if sys.hexversion >= 0x02020000:
__all__.append("finditer")
! def finditer(pattern, string, flags=0, pos=0, endpos=sys.maxint):
"""Return an iterator over all non-overlapping matches in the
string. For each match, the iterator returns a match object.
Empty matches are included in the result."""
! return _compile(pattern, flags).finditer(string, pos, endpos)
def compile(pattern, flags=0):
"Compile a regular expression pattern, returning a pattern object."
-------------- next part --------------
*** libre.tex Wed Sep 8 01:09:55 2004
--- /home/noam/python/old/libre.tex Wed Sep 8 01:04:53 2004
***************
*** 508,530 ****
\end{datadesc}
! \begin{funcdesc}{search}{pattern, string\optional{,
! flags\optional{, pos\optional{, endpos}}}}
Scan through \var{string} looking for a location where the regular
expression \var{pattern} produces a match, and return a
corresponding \class{MatchObject} instance.
Return \code{None} if no
position in the string matches the pattern; note that this is
different from finding a zero-length match at some point in the string.
-
- The optional \var{pos} and \var{endpos} parameters have the same
- meaning as for the \function{match()} function.
- \versionchanged[Added the optional parameters
- \var{pos} and \var{endpos}]{2.4}
\end{funcdesc}
! \begin{funcdesc}{match}{pattern, string\optional{, flags\optional{,
! pos\optional{, endpos}}}}
If zero or more characters at the beginning of \var{string} match
the regular expression \var{pattern}, return a corresponding
\class{MatchObject} instance. Return \code{None} if the string does not
--- 508,523 ----
\end{datadesc}
! \begin{funcdesc}{search}{pattern, string\optional{, flags}}
Scan through \var{string} looking for a location where the regular
expression \var{pattern} produces a match, and return a
corresponding \class{MatchObject} instance.
Return \code{None} if no
position in the string matches the pattern; note that this is
different from finding a zero-length match at some point in the string.
\end{funcdesc}
! \begin{funcdesc}{match}{pattern, string\optional{, flags}}
If zero or more characters at the beginning of \var{string} match
the regular expression \var{pattern}, return a corresponding
\class{MatchObject} instance. Return \code{None} if the string does not
***************
*** 533,561 ****
\note{If you want to locate a match anywhere in
\var{string}, use \method{search()} instead.}
-
- The optional parameter \var{pos} gives an index in the string
- where the search is to start; it defaults to \code{0}. This is not
- completely equivalent to slicing the string; the
- \code{'\textasciicircum'} pattern
- character matches at the real beginning of the string and at positions
- just after a newline, but not necessarily at the index where the search
- is to start.
-
- The optional parameter \var{endpos} limits how far the string will
- be searched; it will be as if the string is \var{endpos} characters
- long, so only the characters from \var{pos} to \code{\var{endpos} -
- 1} will be searched for a match. If \var{endpos} is less than
- \var{pos}, no match will be found, otherwise,
- \code{re.match(\var{string}, \var{pos}=0, \var{endpos}=50)} is
- equivalent to \code{re.match(\var{string}[:50], \var{pos}=0)}.
-
- \versionchanged[Added the optional parameters
- \var{pos} and \var{endpos}]{2.4}
\end{funcdesc}
! \begin{funcdesc}{split}{pattern, string\optional{,
! maxsplit\code{ = 0}\optional{, flags}}}
Split \var{string} by the occurrences of \var{pattern}. If
capturing parentheses are used in \var{pattern}, then the text of all
groups in the pattern are also returned as part of the resulting list.
--- 526,534 ----
\note{If you want to locate a match anywhere in
\var{string}, use \method{search()} instead.}
\end{funcdesc}
! \begin{funcdesc}{split}{pattern, string\optional{, maxsplit\code{ = 0}}}
Split \var{string} by the occurrences of \var{pattern}. If
capturing parentheses are used in \var{pattern}, then the text of all
groups in the pattern are also returned as part of the resulting list.
***************
*** 576,613 ****
This function combines and extends the functionality of
the old \function{regsub.split()} and \function{regsub.splitx()}.
- \versionchanged[Added the optional parameter \var{flags}]{2.4}
\end{funcdesc}
! \begin{funcdesc}{findall}{pattern, string\optional{,
! flags\optional{, pos\optional{, endpos}}}}
Return a list of all non-overlapping matches of \var{pattern} in
\var{string}. If one or more groups are present in the pattern,
return a list of groups; this will be a list of tuples if the
pattern has more than one group. Empty matches are included in the
result unless they touch the beginning of another match.
-
- The optional parameters \var{pos} and \var{endpos} limit the search to
- a part of the string, just like they do in the \function{match()}
- function.
\versionadded{1.5.2}
- \versionchanged[Added the optional parameters
- \var{flags}, \var{pos} and \var{endpos}]{2.4}
\end{funcdesc}
! \begin{funcdesc}{finditer}{pattern, string\optional{,
! flags\optional{, pos\optional{, endpos}}}}
Return an iterator over all non-overlapping matches for the RE
\var{pattern} in \var{string}. For each match, the iterator returns
a match object. Empty matches are included in the result unless they
touch the beginning of another match.
\versionadded{2.2}
- \versionchanged[Added the optional parameters
- \var{flags}, \var{pos} and \var{endpos}]{2.4}
\end{funcdesc}
! \begin{funcdesc}{sub}{pattern, repl, string\optional{,
! count\optional{, flags}}}
Return the string obtained by replacing the leftmost non-overlapping
occurrences of \var{pattern} in \var{string} by the replacement
\var{repl}. If the pattern isn't found, \var{string} is returned
--- 549,574 ----
This function combines and extends the functionality of
the old \function{regsub.split()} and \function{regsub.splitx()}.
\end{funcdesc}
! \begin{funcdesc}{findall}{pattern, string}
Return a list of all non-overlapping matches of \var{pattern} in
\var{string}. If one or more groups are present in the pattern,
return a list of groups; this will be a list of tuples if the
pattern has more than one group. Empty matches are included in the
result unless they touch the beginning of another match.
\versionadded{1.5.2}
\end{funcdesc}
! \begin{funcdesc}{finditer}{pattern, string}
Return an iterator over all non-overlapping matches for the RE
\var{pattern} in \var{string}. For each match, the iterator returns
a match object. Empty matches are included in the result unless they
touch the beginning of another match.
\versionadded{2.2}
\end{funcdesc}
! \begin{funcdesc}{sub}{pattern, repl, string\optional{, count}}
Return the string obtained by replacing the leftmost non-overlapping
occurrences of \var{pattern} in \var{string} by the replacement
\var{repl}. If the pattern isn't found, \var{string} is returned
***************
*** 660,674 ****
group 2 followed by the literal character \character{0}. The
backreference \samp{\e g<0>} substitutes in the entire substring
matched by the RE.
-
- \versionchanged[Added the optional parameter \var{flags}]{2.4}
\end{funcdesc}
! \begin{funcdesc}{subn}{pattern, repl, string\optional{,
! count\optional{, flags}}}
Perform the same operation as \function{sub()}, but return a tuple
\code{(\var{new_string}, \var{number_of_subs_made})}.
- \versionchanged[Added the optional parameter \var{flags}]{2.4}
\end{funcdesc}
\begin{funcdesc}{escape}{string}
--- 621,631 ----
group 2 followed by the literal character \character{0}. The
backreference \samp{\e g<0>} substitutes in the entire substring
matched by the RE.
\end{funcdesc}
! \begin{funcdesc}{subn}{pattern, repl, string\optional{, count}}
Perform the same operation as \function{sub()}, but return a tuple
\code{(\var{new_string}, \var{number_of_subs_made})}.
\end{funcdesc}
\begin{funcdesc}{escape}{string}
***************
*** 737,749 ****
Identical to the \function{split()} function, using the compiled pattern.
\end{methoddesc}
! \begin{methoddesc}[RegexObject]{findall}{string\optional{,
! pos\optional{, endpos}}}
Identical to the \function{findall()} function, using the compiled pattern.
\end{methoddesc}
! \begin{methoddesc}[RegexObject]{finditer}{string\optional{,
! pos\optional{, endpos}}}
Identical to the \function{finditer()} function, using the compiled pattern.
\end{methoddesc}
--- 694,704 ----
Identical to the \function{split()} function, using the compiled pattern.
\end{methoddesc}
! \begin{methoddesc}[RegexObject]{findall}{string}
Identical to the \function{findall()} function, using the compiled pattern.
\end{methoddesc}
! \begin{methoddesc}[RegexObject]{finditer}{string}
Identical to the \function{finditer()} function, using the compiled pattern.
\end{methoddesc}
More information about the Python-Dev
mailing list