]> granicus.if.org Git - python/commitdiff
Merged revisions 70090 via svnmerge from
authorGregory P. Smith <greg@mad-scientist.com>
Mon, 2 Mar 2009 05:21:55 +0000 (05:21 +0000)
committerGregory P. Smith <greg@mad-scientist.com>
Mon, 2 Mar 2009 05:21:55 +0000 (05:21 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r70090 | gregory.p.smith | 2009-03-01 21:13:57 -0800 (Sun, 01 Mar 2009) | 3 lines

  Adds an optional flags argument to re.split, re.sub and re.subn to be
  consistent with the other re module functions.
........

Doc/library/re.rst
Lib/re.py
Misc/NEWS

index 9399a49e148630ed5a1bbd24c864a4c11eba348d..ed50ddb0f7e6de5a323140ccd27fb2038a48f052 100644 (file)
@@ -571,7 +571,7 @@ form.
       instead.
 
 
-.. function:: split(pattern, string[, maxsplit=0])
+.. function:: split(pattern, string[, maxsplit=0, flags=0])
 
    Split *string* by the occurrences of *pattern*.  If capturing parentheses are
    used in *pattern*, then the text of all groups in the pattern are also returned
@@ -585,6 +585,8 @@ form.
       ['Words', ', ', 'words', ', ', 'words', '.', '']
       >>> re.split('\W+', 'Words, words, words.', 1)
       ['Words', 'words, words.']
+      >>> re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE)
+      ['0', '3', '9']
 
    If there are capturing groups in the separator and it matches at the start of
    the string, the result will start with an empty string.  The same holds for
@@ -605,6 +607,9 @@ form.
       >>> re.split("(?m)^$", "foo\n\nbar\n")
       ['foo\n\nbar\n']
 
+   .. versionchanged:: 2.7,3.1
+      Added the optional flags argument.
+
 
 .. function:: findall(pattern, string[, flags])
 
@@ -625,7 +630,7 @@ form.
    match.
 
 
-.. function:: sub(pattern, repl, string[, count])
+.. function:: sub(pattern, repl, string[, count, flags])
 
    Return the string obtained by replacing the leftmost non-overlapping occurrences
    of *pattern* in *string* by the replacement *repl*.  If the pattern isn't found,
@@ -650,6 +655,8 @@ form.
       ...     else: return '-'
       >>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
       'pro--gram files'
+      >>> re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)
+      'Baked Beans & Spam'
 
    The pattern may be a string or an RE object; if you need to specify regular
    expression flags, you must use a RE object, or use embedded modifiers in a
@@ -670,12 +677,18 @@ form.
    character ``'0'``.  The backreference ``\g<0>`` substitutes in the entire
    substring matched by the RE.
 
+   .. versionchanged:: 2.7,3.1
+      Added the optional flags argument.
+
 
-.. function:: subn(pattern, repl, string[, count])
+.. function:: subn(pattern, repl, string[, count, flags])
 
    Perform the same operation as :func:`sub`, but return a tuple ``(new_string,
    number_of_subs_made)``.
 
+   .. versionchanged:: 2.7,3.1
+      Added the optional flags argument.
+
 
 .. function:: escape(string)
 
index d2fc25da372383bb993fd0a6154d28df09535ef0..9bd913a68ddbcfa7ee07c0ad6a47ad6a3ce3cad0 100644 (file)
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -156,16 +156,16 @@ def search(pattern, string, flags=0):
     a match object, or None if no match was found."""
     return _compile(pattern, flags).search(string)
 
-def sub(pattern, repl, string, count=0):
+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 string, backslash escapes in it are processed.  If it is
     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)
+    return _compile(pattern, flags).sub(repl, string, count)
 
-def subn(pattern, repl, string, count=0):
+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
@@ -174,12 +174,12 @@ def subn(pattern, repl, string, count=0):
     callable; if a string, backslash escapes in it are processed.
     If it is 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)
+    return _compile(pattern, flags).subn(repl, string, count)
 
-def split(pattern, string, maxsplit=0):
+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, 0).split(string, maxsplit)
+    return _compile(pattern, flags).split(string, maxsplit)
 
 def findall(pattern, string, flags=0):
     """Return a list of all non-overlapping matches in the string.
index 7f5a99b16227c37a8d03b4b4d694259f9056a284..0272e796e27e1c929fd04636e18dbbc07df8ab99 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -170,6 +170,8 @@ Core and Builtins
 
 - Issue #4748: Lambda generators no longer return a value.
 
+- The re.sub(), re.subn() and re.split() functions now accept a flags parameter.
+
 Library
 -------