summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1996-06-11 18:45:15 (GMT)
committerGuido van Rossum <guido@python.org>1996-06-11 18:45:15 (GMT)
commit4cc4ab1735275abdc22b559ca7238e2cf4a4222e (patch)
tree7486a67bc56732a0b7b368d394fe2c28fb154c15 /Lib
parent8775d8b9dc0e3a199b1da57ed481a417f047e1cf (diff)
downloadcpython-4cc4ab1735275abdc22b559ca7238e2cf4a4222e.zip
cpython-4cc4ab1735275abdc22b559ca7238e2cf4a4222e.tar.gz
cpython-4cc4ab1735275abdc22b559ca7238e2cf4a4222e.tar.bz2
Add third arg to split(). Add capwords() -- which uses that.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/regsub.py16
1 files changed, 15 insertions, 1 deletions
diff --git a/Lib/regsub.py b/Lib/regsub.py
index 507e542..6dbe8dd 100644
--- a/Lib/regsub.py
+++ b/Lib/regsub.py
@@ -50,8 +50,10 @@ 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.
-def split(str, pat):
+def split(str, pat, retain = 0):
prog = compile(pat)
res = []
start = next = 0
@@ -64,11 +66,23 @@ def split(str, pat):
break
else:
res.append(str[start:a])
+ if retain:
+ res.append(str[a:b])
start = next = b
res.append(str[start:])
return res
+# Capitalize words split using a pattern
+
+def capwords(str, pat):
+ import string
+ words = split(str, pat, 1)
+ for i in range(0, len(words), 2):
+ words[i] = string.capitalize(words[i])
+ return string.joinfields(words, "")
+
+
# Internal subroutines:
# compile(pat): compile a pattern, caching already compiled patterns
# expand(repl, regs, str): expand \digit escapes in replacement string