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

Skip to content

Commit 4cc4ab1

Browse files
committed
Add third arg to split(). Add capwords() -- which uses that.
1 parent 8775d8b commit 4cc4ab1

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

Lib/regsub.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ def gsub(pat, repl, str):
5050
# Split string str in fields separated by delimiters matching pattern
5151
# pat. Only non-empty matches for the pattern are considered, so e.g.
5252
# split('abc', '') returns ['abc'].
53+
# When the optional 3rd argument is true, the separators are also
54+
# inserted to the list.
5355

54-
def split(str, pat):
56+
def split(str, pat, retain = 0):
5557
prog = compile(pat)
5658
res = []
5759
start = next = 0
@@ -64,11 +66,23 @@ def split(str, pat):
6466
break
6567
else:
6668
res.append(str[start:a])
69+
if retain:
70+
res.append(str[a:b])
6771
start = next = b
6872
res.append(str[start:])
6973
return res
7074

7175

76+
# Capitalize words split using a pattern
77+
78+
def capwords(str, pat):
79+
import string
80+
words = split(str, pat, 1)
81+
for i in range(0, len(words), 2):
82+
words[i] = string.capitalize(words[i])
83+
return string.joinfields(words, "")
84+
85+
7286
# Internal subroutines:
7387
# compile(pat): compile a pattern, caching already compiled patterns
7488
# expand(repl, regs, str): expand \digit escapes in replacement string

0 commit comments

Comments
 (0)