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
16 # Replace first occurrence of pattern pat in string str by replacement
17 # repl. If the pattern isn't found, the string is returned unchanged.
18 # The replacement may contain references \digit to subpatterns and
19 # escaped backslashes. The pattern may be a string or an already
22 def sub(pat
, repl
, str):
24 if prog
.search(str) >= 0:
27 str = str[:a
] + expand(repl
, regs
, str) + str[b
:]
31 # Replace all (non-overlapping) occurrences of pattern pat in string
32 # str by replacement repl. The same rules as for sub() apply.
33 # Empty matches for the pattern are replaced only when not adjacent to
34 # a previous match, so e.g. gsub('', '-', 'abc') returns '-a-b-c-'.
36 def gsub(pat
, repl
, str):
41 while prog
.search(str, start
) >= 0:
44 if a
== b
== start
and not first
:
45 if start
>= len(str) or prog
.search(str, start
+1) < 0:
49 new
= new
+ str[start
:a
] + expand(repl
, regs
, str)
52 new
= new
+ str[start
:]
56 # Split string str in fields separated by delimiters matching pattern
57 # pat. Only non-empty matches for the pattern are considered, so e.g.
58 # split('abc', '') returns ['abc'].
59 # The optional 3rd argument sets the number of splits that are performed.
61 def split(str, pat
, maxsplit
= 0):
62 return intsplit(str, pat
, maxsplit
, 0)
64 # Split string str in fields separated by delimiters matching pattern
65 # pat. Only non-empty matches for the pattern are considered, so e.g.
66 # split('abc', '') returns ['abc']. The delimiters are also included
68 # The optional 3rd argument sets the number of splits that are performed.
71 def splitx(str, pat
, maxsplit
= 0):
72 return intsplit(str, pat
, maxsplit
, 1)
74 # Internal function used to implement split() and splitx().
76 def intsplit(str, pat
, maxsplit
, retain
):
81 while prog
.search(str, next
) >= 0:
89 res
.append(str[start
:a
])
93 splitcount
= splitcount
+ 1
94 if (maxsplit
and (splitcount
>= maxsplit
)):
96 res
.append(str[start
:])
100 # Capitalize words split using a pattern
102 def capwords(str, pat
='[^a-zA-Z0-9_]+'):
104 words
= splitx(str, pat
)
105 for i
in range(0, len(words
), 2):
106 words
[i
] = string
.capitalize(words
[i
])
107 return string
.joinfields(words
, "")
110 # Internal subroutines:
111 # compile(pat): compile a pattern, caching already compiled patterns
112 # expand(repl, regs, str): expand \digit escapes in replacement string
115 # Manage a cache of compiled regular expressions.
117 # If the pattern is a string a compiled version of it is returned. If
118 # the pattern has been used before we return an already compiled
119 # version from the cache; otherwise we compile it now and save the
120 # compiled version in the cache, along with the syntax it was compiled
121 # with. Instead of a string, a compiled regular expression can also
127 if type(pat
) <> type(''):
128 return pat
# Assume it is a compiled regex
129 key
= (pat
, regex
.get_syntax())
130 if cache
.has_key(key
):
131 prog
= cache
[key
] # Get it from the cache
133 prog
= cache
[key
] = regex
.compile(pat
)
142 # Expand \digit in the replacement.
143 # Each occurrence of \digit is replaced by the substring of str
144 # indicated by regs[digit]. To include a literal \ in the
145 # replacement, double it; other \ escapes are left unchanged (i.e.
146 # the \ and the following character are both copied).
148 def expand(repl
, regs
, str):
156 if c
<> '\\' or i
>= len(repl
):
161 a
, b
= regs
[ord(c
)-ord0
]
170 # Test program, reads sequences "pat repl str" from stdin.
171 # Optional argument specifies pattern used to split lines.
180 if sys
.stdin
.isatty(): sys
.stderr
.write('--> ')
181 line
= sys
.stdin
.readline()
183 if line
[-1] == '\n': line
= line
[:-1]
184 fields
= split(line
, delpat
)
186 print 'Sorry, not three fields'
187 print 'split:', `fields`
189 [pat
, repl
, str] = split(line
, delpat
)
190 print 'sub :', `
sub(pat
, repl
, str)`
191 print 'gsub:', `
gsub(pat
, repl
, str)`