1 """Regexp-based split and replace using the obsolete regex module.
3 This module is only for backward compatibility. These operations
4 are now provided by the new regular expression module, "re".
6 sub(pat, repl, str): replace first occurrence of pattern in string
7 gsub(pat, repl, str): replace all occurrences of pattern in string
8 split(str, pat, maxsplit): split string using pattern as delimiter
9 splitx(str, pat, maxsplit): split string using pattern as delimiter plus
14 warnings.warn(
"the regsub module is deprecated; please use re.sub()",
22 __all__ = [
"sub",
"gsub",
"split",
"splitx",
"capwords"]
30 def sub(pat, repl, str):
32 if prog.search(str) >= 0:
35 str = str[:a] +
expand(repl, regs, str) + str[b:]
49 while prog.search(str, start) >= 0:
52 if a == b == start
and not first:
53 if start >= len(str)
or prog.search(str, start+1) < 0:
57 new = new + str[start:a] +
expand(repl, regs, str)
60 new = new + str[start:]
69 def split(str, pat, maxsplit = 0):
70 return intsplit(str, pat, maxsplit, 0)
80 return intsplit(str, pat, maxsplit, 1)
89 while prog.search(str, next) >= 0:
97 res.append(str[start:a])
101 splitcount = splitcount + 1
102 if (maxsplit
and (splitcount >= maxsplit)):
104 res.append(str[start:])
112 for i
in range(0, len(words), 2):
114 return "".
join(words)
134 if type(pat) != type(
''):
136 key = (pat, regex.get_syntax())
137 if cache.has_key(key):
140 prog = cache[key] = regex.compile(pat)
163 if c !=
'\\' or i >= len(repl):
168 a, b = regs[ord(c)-ord0]
187 if sys.stdin.isatty(): sys.stderr.write(
'--> ')
188 line = sys.stdin.readline()
190 if line[-1] ==
'\n': line = line[:-1]
191 fields =
split(line, delpat)
193 print 'Sorry, not three fields'
194 print 'split:', `fields`
196 [pat, repl, str] =
split(line, delpat)
197 print 'sub :', `
sub(pat, repl, str)`
198 print 'gsub:', `
gsub(pat, repl, str)`