summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1996-08-08 18:39:18 (GMT)
committerGuido van Rossum <guido@python.org>1996-08-08 18:39:18 (GMT)
commita59d3e6d507fe5dbe2583a23ab8f2cb631ebcafe (patch)
tree88ac6fbd27bd3df797caa943b5a31193e0c88150 /Lib
parent37a6f16d2ec04facf3225e05972b42ed7ece1407 (diff)
downloadcpython-a59d3e6d507fe5dbe2583a23ab8f2cb631ebcafe.zip
cpython-a59d3e6d507fe5dbe2583a23ab8f2cb631ebcafe.tar.gz
cpython-a59d3e6d507fe5dbe2583a23ab8f2cb631ebcafe.tar.bz2
Changed split() to be compatible with changes to string.split(): the
optional third argument gives a maximum number of delimiters to parse. The new function splitx() is like split() but returns a list containing the words as well as the delimiters.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/regsub.py28
1 files changed, 24 insertions, 4 deletions
diff --git a/Lib/regsub.py b/Lib/regsub.py
index 6dbe8dd..119dacf 100644
--- a/Lib/regsub.py
+++ b/Lib/regsub.py
@@ -1,7 +1,9 @@
# Regular expression subroutines:
# sub(pat, repl, str): replace first occurrence of pattern in string
# gsub(pat, repl, str): replace all occurrences of pattern in string
-# split(str, pat): split string using pattern as delimiter
+# split(str, pat, maxsplit): split string using pattern as delimiter
+# splitx(str, pat, maxsplit): split string using pattern as delimiter plus
+# return delimiters
import regex
@@ -50,13 +52,28 @@ def gsub(pat, repl, str):
# Split string str in fields separated by delimiters matching pattern
# pat. Only non-empty matches for the pattern are considered, so e.g.
# split('abc', '') returns ['abc'].
-# When the optional 3rd argument is true, the separators are also
-# inserted to the list.
+# The optional 3rd argument sets the number of splits that are performed.
-def split(str, pat, retain = 0):
+def split(str, pat, maxsplit = 0):
+ return intsplit(str, pat, maxsplit, 0)
+
+# Split string str in fields separated by delimiters matching pattern
+# pat. Only non-empty matches for the pattern are considered, so e.g.
+# split('abc', '') returns ['abc']. The delimiters are also included
+# in the list.
+# The optional 3rd argument sets the number of splits that are performed.
+
+
+def splitx(str, pat, maxsplit = 0):
+ return intsplit(str, pat, maxsplit, 1)
+
+# Internal function used to implement split() and splitx().
+
+def intsplit(str, pat, maxsplit, retain):
prog = compile(pat)
res = []
start = next = 0
+ splitcount = 0
while prog.search(str, next) >= 0:
regs = prog.regs
a, b = regs[0]
@@ -69,6 +86,9 @@ def split(str, pat, retain = 0):
if retain:
res.append(str[a:b])
start = next = b
+ splitcount = splitcount + 1
+ if (maxsplit and (splitcount >= maxsplit)):
+ break
res.append(str[start:])
return res