Mercurial > cpython-lru_cache-weakref
annotate Lib/regsub.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 3f57ab32b741 |
| children | 0bf7394dba22 |
| 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. |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 # If the pattern is a string a compiled version of it is returned. |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 # If the pattern has been used before we return an already compiled |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 # version from the cache; otherwise we compile it now and save the |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 # compiled version in the cache. |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 # Instead of a string, a compiled regular expression can also be |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 # passed. |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 # WARNING: if the pattern syntax is changed, the cache should be |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 # flushed! |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 cache = {} |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 def compile(pat): |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 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
|
125 return pat # Assume it is a compiled regex |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 if cache.has_key(pat): |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 prog = cache[pat] # Get it from the cache |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 else: |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 prog = cache[pat] = regex.compile(pat) |
|
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 |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 # 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
|
134 # 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
|
135 # 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
|
136 # 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
|
137 # 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
|
138 |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 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
|
140 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
|
141 return repl |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 new = '' |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 i = 0 |
|
3636
6768bbd18e09
avoid eval() like the plague
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
144 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
|
145 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
|
146 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
|
147 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
|
148 new = new + c |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 else: |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 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
|
151 if '0' <= c <= '9': |
|
3636
6768bbd18e09
avoid eval() like the plague
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
152 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
|
153 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
|
154 elif c == '\\': |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 new = new + c |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 else: |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 new = new + '\\' + c |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 return new |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 # 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
|
162 # 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
|
163 |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 def test(): |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 import sys |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 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
|
167 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
|
168 else: |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 delpat = '[ \t\n]+' |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 while 1: |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 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
|
177 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
|
178 print 'split:', `fields` |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 continue |
|
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 [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
|
181 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
|
182 print 'gsub:', `gsub(pat, repl, str)` |
