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