annotate Lib/regsub.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 0bf7394dba22
children 6f4146846337
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Regular expression subroutines:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # sub(pat, repl, str): replace first occurrence of pattern in string
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # gsub(pat, repl, str): replace all occurrences of pattern in string
3956
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
4 # split(str, pat, maxsplit): split string using pattern as delimiter
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
5 # splitx(str, pat, maxsplit): split string using pattern as delimiter plus
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
6 # return delimiters
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 import regex
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # Replace first occurrence of pattern pat in string str by replacement
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # repl. If the pattern isn't found, the string is returned unchanged.
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # The replacement may contain references \digit to subpatterns and
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # escaped backslashes. The pattern may be a string or an already
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # compiled pattern.
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 def sub(pat, repl, str):
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 prog = compile(pat)
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 if prog.search(str) >= 0:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 regs = prog.regs
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 a, b = regs[0]
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 str = str[:a] + expand(repl, regs, str) + str[b:]
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 return str
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # Replace all (non-overlapping) occurrences of pattern pat in string
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # str by replacement repl. The same rules as for sub() apply.
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # Empty matches for the pattern are replaced only when not adjacent to
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # a previous match, so e.g. gsub('', '-', 'abc') returns '-a-b-c-'.
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 def gsub(pat, repl, str):
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 prog = compile(pat)
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 new = ''
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 start = 0
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 first = 1
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 while prog.search(str, start) >= 0:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 regs = prog.regs
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 a, b = regs[0]
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 if a == b == start and not first:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 if start >= len(str) or prog.search(str, start+1) < 0:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 break
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 regs = prog.regs
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 a, b = regs[0]
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 new = new + str[start:a] + expand(repl, regs, str)
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 start = b
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 first = 0
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 new = new + str[start:]
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 return new
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 # Split string str in fields separated by delimiters matching pattern
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 # pat. Only non-empty matches for the pattern are considered, so e.g.
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 # split('abc', '') returns ['abc'].
3956
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
55 # The optional 3rd argument sets the number of splits that are performed.
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
56
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
57 def split(str, pat, maxsplit = 0):
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
58 return intsplit(str, pat, maxsplit, 0)
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
3956
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
60 # Split string str in fields separated by delimiters matching pattern
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
61 # pat. Only non-empty matches for the pattern are considered, so e.g.
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
62 # split('abc', '') returns ['abc']. The delimiters are also included
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
63 # in the list.
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
64 # The optional 3rd argument sets the number of splits that are performed.
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
65
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
66
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
67 def splitx(str, pat, maxsplit = 0):
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
68 return intsplit(str, pat, maxsplit, 1)
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
69
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
70 # Internal function used to implement split() and splitx().
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
71
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
72 def intsplit(str, pat, maxsplit, retain):
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 prog = compile(pat)
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 res = []
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 start = next = 0
3956
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
76 splitcount = 0
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 while prog.search(str, next) >= 0:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 regs = prog.regs
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 a, b = regs[0]
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 if a == b:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 next = next + 1
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 if next >= len(str):
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 break
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 else:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 res.append(str[start:a])
3670
16413ee58967 Add third arg to split(). Add capwords() -- which uses that.
Guido van Rossum <guido@python.org>
parents: 3636
diff changeset
86 if retain:
16413ee58967 Add third arg to split(). Add capwords() -- which uses that.
Guido van Rossum <guido@python.org>
parents: 3636
diff changeset
87 res.append(str[a:b])
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 start = next = b
3956
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
89 splitcount = splitcount + 1
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
90 if (maxsplit and (splitcount >= maxsplit)):
8e309be89ee4 Changed split() to be compatible with changes to string.split(): the
Guido van Rossum <guido@python.org>
parents: 3670
diff changeset
91 break
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 res.append(str[start:])
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 return res
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
3670
16413ee58967 Add third arg to split(). Add capwords() -- which uses that.
Guido van Rossum <guido@python.org>
parents: 3636
diff changeset
96 # Capitalize words split using a pattern
16413ee58967 Add third arg to split(). Add capwords() -- which uses that.
Guido van Rossum <guido@python.org>
parents: 3636
diff changeset
97
3994
3f57ab32b741 Use splitx() in capwords() (bugfix after interface change for split()).
Guido van Rossum <guido@python.org>
parents: 3956
diff changeset
98 def capwords(str, pat='[^a-zA-Z0-9_]+'):
3670
16413ee58967 Add third arg to split(). Add capwords() -- which uses that.
Guido van Rossum <guido@python.org>
parents: 3636
diff changeset
99 import string
3994
3f57ab32b741 Use splitx() in capwords() (bugfix after interface change for split()).
Guido van Rossum <guido@python.org>
parents: 3956
diff changeset
100 words = splitx(str, pat)
3670
16413ee58967 Add third arg to split(). Add capwords() -- which uses that.
Guido van Rossum <guido@python.org>
parents: 3636
diff changeset
101 for i in range(0, len(words), 2):
16413ee58967 Add third arg to split(). Add capwords() -- which uses that.
Guido van Rossum <guido@python.org>
parents: 3636
diff changeset
102 words[i] = string.capitalize(words[i])
16413ee58967 Add third arg to split(). Add capwords() -- which uses that.
Guido van Rossum <guido@python.org>
parents: 3636
diff changeset
103 return string.joinfields(words, "")
16413ee58967 Add third arg to split(). Add capwords() -- which uses that.
Guido van Rossum <guido@python.org>
parents: 3636
diff changeset
104
16413ee58967 Add third arg to split(). Add capwords() -- which uses that.
Guido van Rossum <guido@python.org>
parents: 3636
diff changeset
105
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 # Internal subroutines:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 # compile(pat): compile a pattern, caching already compiled patterns
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 # expand(repl, regs, str): expand \digit escapes in replacement string
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 # Manage a cache of compiled regular expressions.
5130
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
112 #
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
113 # If the pattern is a string a compiled version of it is returned. If
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
114 # the pattern has been used before we return an already compiled
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 # version from the cache; otherwise we compile it now and save the
5130
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
116 # compiled version in the cache, along with the syntax it was compiled
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
117 # with. Instead of a string, a compiled regular expression can also
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
118 # be passed.
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 cache = {}
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 def compile(pat):
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 if type(pat) <> type(''):
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 return pat # Assume it is a compiled regex
5130
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
125 key = (pat, regex.get_syntax())
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
126 if cache.has_key(key):
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
127 prog = cache[key] # Get it from the cache
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 else:
5130
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
129 prog = cache[key] = regex.compile(pat)
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 return prog
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
5130
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
133 def clear_cache():
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
134 global cache
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
135 cache = {}
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
136
0bf7394dba22 Store the current regex syntax along with the regular expression
Barry Warsaw <barry@python.org>
parents: 3994
diff changeset
137
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 # Expand \digit in the replacement.
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 # Each occurrence of \digit is replaced by the substring of str
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 # indicated by regs[digit]. To include a literal \ in the
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 # replacement, double it; other \ escapes are left unchanged (i.e.
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 # the \ and the following character are both copied).
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 def expand(repl, regs, str):
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 if '\\' not in repl:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 return repl
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 new = ''
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 i = 0
3636
6768bbd18e09 avoid eval() like the plague
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
149 ord0 = ord('0')
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 while i < len(repl):
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 c = repl[i]; i = i+1
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 if c <> '\\' or i >= len(repl):
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 new = new + c
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 else:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 c = repl[i]; i = i+1
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 if '0' <= c <= '9':
3636
6768bbd18e09 avoid eval() like the plague
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
157 a, b = regs[ord(c)-ord0]
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 new = new + str[a:b]
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 elif c == '\\':
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 new = new + c
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 else:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 new = new + '\\' + c
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 return new
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 # Test program, reads sequences "pat repl str" from stdin.
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 # Optional argument specifies pattern used to split lines.
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 def test():
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 import sys
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 if sys.argv[1:]:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 delpat = sys.argv[1]
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 else:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 delpat = '[ \t\n]+'
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 while 1:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 if sys.stdin.isatty(): sys.stderr.write('--> ')
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 line = sys.stdin.readline()
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 if not line: break
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 if line[-1] == '\n': line = line[:-1]
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 fields = split(line, delpat)
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 if len(fields) <> 3:
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 print 'Sorry, not three fields'
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 print 'split:', `fields`
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 continue
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 [pat, repl, str] = split(line, delpat)
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 print 'sub :', `sub(pat, repl, str)`
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 print 'gsub:', `gsub(pat, repl, str)`