|
3 | 3 | # |
4 | 4 | # re-compatible interface for the sre matching engine |
5 | 5 | # |
6 | | -# Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved. |
| 6 | +# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved. |
7 | 7 | # |
8 | 8 | # This version of the SRE library can be redistributed under CNRI's |
9 | 9 | # Python 1.6 license. For any other use, please contact Secret Labs |
|
14 | 14 | # other compatibility work. |
15 | 15 | # |
16 | 16 |
|
17 | | -# FIXME: change all FIXME's to XXX ;-) |
18 | | - |
19 | 17 | import sre_compile |
20 | 18 | import sre_parse |
21 | 19 |
|
22 | 20 | import string |
23 | 21 |
|
24 | 22 | # flags |
25 | | -I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE |
26 | | -L = LOCALE = sre_compile.SRE_FLAG_LOCALE |
27 | | -M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE |
28 | | -S = DOTALL = sre_compile.SRE_FLAG_DOTALL |
29 | | -X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE |
| 23 | +I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case |
| 24 | +L = LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale |
| 25 | +U = UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode locale |
| 26 | +M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline |
| 27 | +S = DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline |
| 28 | +X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments |
30 | 29 |
|
31 | | -# sre extensions (may or may not be in 1.6/2.0 final) |
32 | | -T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE |
33 | | -U = UNICODE = sre_compile.SRE_FLAG_UNICODE |
| 30 | +# sre extensions (experimental, don't rely on these) |
| 31 | +T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking |
| 32 | +DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation |
34 | 33 |
|
35 | 34 | # sre exception |
36 | 35 | error = sre_compile.error |
37 | 36 |
|
38 | 37 | # -------------------------------------------------------------------- |
39 | 38 | # public interface |
40 | 39 |
|
41 | | -# FIXME: add docstrings |
42 | | - |
43 | 40 | def match(pattern, string, flags=0): |
| 41 | + """Try to apply the pattern at the start of the string, returning |
| 42 | + a match object, or None if no match was found.""" |
44 | 43 | return _compile(pattern, flags).match(string) |
45 | 44 |
|
46 | 45 | def search(pattern, string, flags=0): |
| 46 | + """Scan through string looking for a match to the pattern, returning |
| 47 | + a match object, or None if no match was found.""" |
47 | 48 | return _compile(pattern, flags).search(string) |
48 | 49 |
|
49 | 50 | def sub(pattern, repl, string, count=0): |
| 51 | + """Return the string obtained by replacing the leftmost |
| 52 | + non-overlapping occurrences of the pattern in string by the |
| 53 | + replacement repl""" |
50 | 54 | return _compile(pattern, 0).sub(repl, string, count) |
51 | 55 |
|
52 | 56 | def subn(pattern, repl, string, count=0): |
| 57 | + """Return a 2-tuple containing (new_string, number). |
| 58 | + new_string is the string obtained by replacing the leftmost |
| 59 | + non-overlapping occurrences of the pattern in the source |
| 60 | + string by the replacement repl. number is the number of |
| 61 | + substitutions that were made.""" |
53 | 62 | return _compile(pattern, 0).subn(repl, string, count) |
54 | 63 |
|
55 | 64 | def split(pattern, string, maxsplit=0): |
| 65 | + """Split the source string by the occurrences of the pattern, |
| 66 | + returning a list containing the resulting substrings.""" |
56 | 67 | return _compile(pattern, 0).split(string, maxsplit) |
57 | 68 |
|
58 | 69 | def findall(pattern, string, maxsplit=0): |
| 70 | + """Return a list of all non-overlapping matches in the string. |
| 71 | +
|
| 72 | + If one or more groups are present in the pattern, return a |
| 73 | + list of groups; this will be a list of tuples if the pattern |
| 74 | + has more than one group. |
| 75 | +
|
| 76 | + Empty matches are included in the result.""" |
59 | 77 | return _compile(pattern, 0).findall(string, maxsplit) |
60 | 78 |
|
61 | 79 | def compile(pattern, flags=0): |
| 80 | + "Compile a regular expression pattern, returning a pattern object." |
62 | 81 | return _compile(pattern, flags) |
63 | 82 |
|
64 | 83 | def purge(): |
| 84 | + "Clear the regular expression cache" |
65 | 85 | _cache.clear() |
66 | 86 |
|
67 | 87 | def template(pattern, flags=0): |
| 88 | + "Compile a template pattern, returning a pattern object" |
| 89 | + |
68 | 90 | return _compile(pattern, flags|T) |
69 | 91 |
|
70 | 92 | def escape(pattern): |
| 93 | + "Escape all non-alphanumeric characters in pattern." |
71 | 94 | s = list(pattern) |
72 | 95 | for i in range(len(pattern)): |
73 | 96 | c = pattern[i] |
@@ -204,7 +227,7 @@ def scan(self, string): |
204 | 227 | break |
205 | 228 | action = self.lexicon[m.lastindex][1] |
206 | 229 | if callable(action): |
207 | | - self.match = match |
| 230 | + self.match = m |
208 | 231 | action = action(self, m.group()) |
209 | 232 | if action is not None: |
210 | 233 | append(action) |
|
0 commit comments