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

Skip to content

Commit ccc5ae7

Browse files
committed
Merged revisions 70090 via svnmerge from
svn+ssh://[email protected]/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. ........
1 parent 4221c74 commit ccc5ae7

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

Doc/library/re.rst

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ form.
571571
instead.
572572

573573

574-
.. function:: split(pattern, string[, maxsplit=0])
574+
.. function:: split(pattern, string[, maxsplit=0, flags=0])
575575

576576
Split *string* by the occurrences of *pattern*. If capturing parentheses are
577577
used in *pattern*, then the text of all groups in the pattern are also returned
@@ -585,6 +585,8 @@ form.
585585
['Words', ', ', 'words', ', ', 'words', '.', '']
586586
>>> re.split('\W+', 'Words, words, words.', 1)
587587
['Words', 'words, words.']
588+
>>> re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE)
589+
['0', '3', '9']
588590

589591
If there are capturing groups in the separator and it matches at the start of
590592
the string, the result will start with an empty string. The same holds for
@@ -605,6 +607,9 @@ form.
605607
>>> re.split("(?m)^$", "foo\n\nbar\n")
606608
['foo\n\nbar\n']
607609

610+
.. versionchanged:: 2.7,3.1
611+
Added the optional flags argument.
612+
608613

609614
.. function:: findall(pattern, string[, flags])
610615

@@ -625,7 +630,7 @@ form.
625630
match.
626631

627632

628-
.. function:: sub(pattern, repl, string[, count])
633+
.. function:: sub(pattern, repl, string[, count, flags])
629634

630635
Return the string obtained by replacing the leftmost non-overlapping occurrences
631636
of *pattern* in *string* by the replacement *repl*. If the pattern isn't found,
@@ -650,6 +655,8 @@ form.
650655
... else: return '-'
651656
>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
652657
'pro--gram files'
658+
>>> re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)
659+
'Baked Beans & Spam'
653660

654661
The pattern may be a string or an RE object; if you need to specify regular
655662
expression flags, you must use a RE object, or use embedded modifiers in a
@@ -670,12 +677,18 @@ form.
670677
character ``'0'``. The backreference ``\g<0>`` substitutes in the entire
671678
substring matched by the RE.
672679

680+
.. versionchanged:: 2.7,3.1
681+
Added the optional flags argument.
682+
673683

674-
.. function:: subn(pattern, repl, string[, count])
684+
.. function:: subn(pattern, repl, string[, count, flags])
675685

676686
Perform the same operation as :func:`sub`, but return a tuple ``(new_string,
677687
number_of_subs_made)``.
678688

689+
.. versionchanged:: 2.7,3.1
690+
Added the optional flags argument.
691+
679692

680693
.. function:: escape(string)
681694

Lib/re.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,16 @@ def search(pattern, string, flags=0):
156156
a match object, or None if no match was found."""
157157
return _compile(pattern, flags).search(string)
158158

159-
def sub(pattern, repl, string, count=0):
159+
def sub(pattern, repl, string, count=0, flags=0):
160160
"""Return the string obtained by replacing the leftmost
161161
non-overlapping occurrences of the pattern in string by the
162162
replacement repl. repl can be either a string or a callable;
163163
if a string, backslash escapes in it are processed. If it is
164164
a callable, it's passed the match object and must return
165165
a replacement string to be used."""
166-
return _compile(pattern, 0).sub(repl, string, count)
166+
return _compile(pattern, flags).sub(repl, string, count)
167167

168-
def subn(pattern, repl, string, count=0):
168+
def subn(pattern, repl, string, count=0, flags=0):
169169
"""Return a 2-tuple containing (new_string, number).
170170
new_string is the string obtained by replacing the leftmost
171171
non-overlapping occurrences of the pattern in the source
@@ -174,12 +174,12 @@ def subn(pattern, repl, string, count=0):
174174
callable; if a string, backslash escapes in it are processed.
175175
If it is a callable, it's passed the match object and must
176176
return a replacement string to be used."""
177-
return _compile(pattern, 0).subn(repl, string, count)
177+
return _compile(pattern, flags).subn(repl, string, count)
178178

179-
def split(pattern, string, maxsplit=0):
179+
def split(pattern, string, maxsplit=0, flags=0):
180180
"""Split the source string by the occurrences of the pattern,
181181
returning a list containing the resulting substrings."""
182-
return _compile(pattern, 0).split(string, maxsplit)
182+
return _compile(pattern, flags).split(string, maxsplit)
183183

184184
def findall(pattern, string, flags=0):
185185
"""Return a list of all non-overlapping matches in the string.

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ Core and Builtins
170170

171171
- Issue #4748: Lambda generators no longer return a value.
172172

173+
- The re.sub(), re.subn() and re.split() functions now accept a flags parameter.
174+
173175
Library
174176
-------
175177

0 commit comments

Comments
 (0)