Mercurial > cpython-lru_cache-weakref
annotate Lib/pre.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | a0dc6f504972 |
| children |
| rev | line source |
|---|---|
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 # module 're' -- A collection of regular expression operations |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
17021
f0efdcd24512
Make docstrings raw, since they contain literal backslashes.
Ka-Ping Yee <ping@zesty.ca>
parents:
16613
diff
changeset
|
3 r"""Support for regular expressions (RE). |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 This module provides regular expression matching operations similar to |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 those found in Perl. It's 8-bit clean: the strings being processed may |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 contain both null bytes and characters whose high bit is set. Regular |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 expression pattern strings may not contain null bytes, but can specify |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 the null byte using the \\number notation. Characters with the high |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 bit set may be included. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 Regular expressions can contain both special and ordinary |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 characters. Most ordinary characters, like "A", "a", or "0", are the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 simplest regular expressions; they simply match themselves. You can |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 concatenate ordinary characters, so last matches the string 'last'. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 The special characters are: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 "." Matches any character except a newline. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 "^" Matches the start of the string. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 "$" Matches the end of the string. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 "*" Matches 0 or more (greedy) repetitions of the preceding RE. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 Greedy means that it will match as many repetitions as possible. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 "+" Matches 1 or more (greedy) repetitions of the preceding RE. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 "?" Matches 0 or 1 (greedy) of the preceding RE. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 *?,+?,?? Non-greedy versions of the previous three special characters. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 {m,n} Matches from m to n repetitions of the preceding RE. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 {m,n}? Non-greedy version of the above. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 "\\" Either escapes special characters or signals a special sequence. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 [] Indicates a set of characters. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 A "^" as the first character indicates a complementing set. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 "|" A|B, creates an RE that will match either A or B. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 (...) Matches the RE inside the parentheses. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 The contents can be retrieved or matched later in the string. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 (?iLmsx) Set the I, L, M, S, or X flag for the RE. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 (?:...) Non-grouping version of regular parentheses. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 (?P<name>...) The substring matched by the group is accessible by name. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 (?P=name) Matches the text matched earlier by the group named name. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 (?#...) A comment; ignored. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 (?=...) Matches if ... matches next, but doesn't consume the string. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 (?!...) Matches if ... doesn't match next. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 The special sequences consist of "\\" and a character from the list |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 below. If the ordinary character is not on the list, then the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 resulting RE will match the second character. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 \\number Matches the contents of the group of the same number. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 \\A Matches only at the start of the string. |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
47 \\Z Matches only at the end of the string. |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 \\b Matches the empty string, but only at the start or end of a word. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 \\B Matches the empty string, but not at the start or end of a word. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 \\d Matches any decimal digit; equivalent to the set [0-9]. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 \\D Matches any non-digit character; equivalent to the set [^0-9]. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 \\s Matches any whitespace character; equivalent to [ \\t\\n\\r\\f\\v]. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 \\S Matches any non-whitespace character; equiv. to [^ \\t\\n\\r\\f\\v]. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 \\w Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 With LOCALE, it will match the set [0-9_] plus characters defined |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 as letters for the current locale. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 \\W Matches the complement of \\w. |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
58 \\\\ Matches a literal backslash. |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 This module exports the following functions: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 match Match a regular expression pattern to the beginning of a string. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 search Search a string for the presence of a pattern. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 sub Substitute occurrences of a pattern found in a string. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 subn Same as sub, but also return the number of substitutions made. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 split Split a string by the occurrences of a pattern. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 findall Find all occurrences of a pattern in a string. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 compile Compile a pattern into a RegexObject. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 escape Backslash all non-alphanumerics in a string. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 This module exports the following classes: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 RegexObject Holds a compiled regular expression pattern. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 MatchObject Contains information about pattern matches. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 Some of the functions in this module takes flags as optional parameters: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 I IGNORECASE Perform case-insensitive matching. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 L LOCALE Make \w, \W, \b, \B, dependent on the current locale. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 M MULTILINE "^" matches the beginning of lines as well as the string. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 "$" matches the end of lines as well as the string. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 S DOTALL "." matches any character at all, including the newline. |
|
13620
5b727ab37092
Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents:
13229
diff
changeset
|
80 X VERBOSE Ignore whitespace and comments for nicer looking RE's. |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 This module also defines an exception 'error'. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 import sys |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 from pcre import * |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 |
| 16613 | 90 __all__ = ["match","search","sub","subn","split","findall","escape","compile", |
| 91 "I","L","M","S","X","IGNORECASE","LOCALE","MULTILINE","DOTALL", | |
| 92 "VERBOSE","error"] | |
|
16562
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
16548
diff
changeset
|
93 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 # |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 # First, the public part of the interface: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 # |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 # pcre.error and re.error should be the same, since exceptions can be |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 # raised from either module. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 # compilation flags |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 I = IGNORECASE |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 L = LOCALE |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 M = MULTILINE |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
106 S = DOTALL |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
107 X = VERBOSE |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 # |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 # |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 # |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 _cache = {} |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 _MAXCACHE = 20 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 def _cachecompile(pattern, flags=0): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 key = (pattern, flags) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 try: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 return _cache[key] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 except KeyError: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 pass |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 value = compile(pattern, flags) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 if len(_cache) >= _MAXCACHE: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 _cache.clear() |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 _cache[key] = value |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 return value |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 def match(pattern, string, flags=0): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 """match (pattern, string[, flags]) -> MatchObject or None |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
131 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 If zero or more characters at the beginning of string match the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 regular expression pattern, return a corresponding MatchObject |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 instance. Return None if the string does not match the pattern; |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 note that this is different from a zero-length match. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 Note: If you want to locate a match anywhere in string, use |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 search() instead. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 """ |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
141 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 return _cachecompile(pattern, flags).match(string) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
143 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 def search(pattern, string, flags=0): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 """search (pattern, string[, flags]) -> MatchObject or None |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
146 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 Scan through string looking for a location where the regular |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 expression pattern produces a match, and return a corresponding |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 MatchObject instance. Return None if no position in the string |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 matches the pattern; note that this is different from finding a |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 zero-length match at some point in the string. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 return _cachecompile(pattern, flags).search(string) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
155 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 def sub(pattern, repl, string, count=0): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 """sub(pattern, repl, string[, count=0]) -> string |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
158 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 Return the string obtained by replacing the leftmost |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 non-overlapping occurrences of pattern in string by the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 replacement repl. If the pattern isn't found, string is returned |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 unchanged. repl can be a string or a function; if a function, it |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 is called for every non-overlapping occurrence of pattern. The |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 function takes a single match object argument, and returns the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 replacement string. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 The pattern may be a string or a regex object; if you need to |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 specify regular expression flags, you must use a regex object, or |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 use embedded modifiers in a pattern; e.g. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 sub("(?i)b+", "x", "bbbb BBBB") returns 'x x'. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 The optional argument count is the maximum number of pattern |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 occurrences to be replaced; count must be a non-negative integer, |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 and the default value of 0 means to replace all occurrences. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 if type(pattern) == type(''): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 pattern = _cachecompile(pattern) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 return pattern.sub(repl, string, count) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 def subn(pattern, repl, string, count=0): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 """subn(pattern, repl, string[, count=0]) -> (string, num substitutions) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
183 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 Perform the same operation as sub(), but return a tuple |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 (new_string, number_of_subs_made). |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 if type(pattern) == type(''): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 pattern = _cachecompile(pattern) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 return pattern.subn(repl, string, count) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
191 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 def split(pattern, string, maxsplit=0): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 """split(pattern, string[, maxsplit=0]) -> list of strings |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
194 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 Split string by the occurrences of pattern. If capturing |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 parentheses are used in pattern, then the text of all groups in |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 the pattern are also returned as part of the resulting list. If |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 maxsplit is nonzero, at most maxsplit splits occur, and the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 remainder of the string is returned as the final element of the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 list. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 if type(pattern) == type(''): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 pattern = _cachecompile(pattern) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 return pattern.split(string, maxsplit) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 def findall(pattern, string): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 """findall(pattern, string) -> list |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
209 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 Return a list of all non-overlapping matches of pattern in |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 string. If one or more groups are present in the pattern, return a |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 list of groups; this will be a list of tuples if the pattern has |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 more than one group. Empty matches are included in the result. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 if type(pattern) == type(''): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 pattern = _cachecompile(pattern) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 return pattern.findall(string) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 def escape(pattern): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 """escape(string) -> string |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
222 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 Return string with all non-alphanumerics backslashed; this is |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 useful if you want to match an arbitrary literal string that may |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 have regular expression metacharacters in it. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 result = list(pattern) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 for i in range(len(pattern)): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 char = pattern[i] |
| 16548 | 231 if not char.isalnum(): |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 if char=='\000': result[i] = '\\000' |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 else: result[i] = '\\'+char |
|
16499
58de2829cfdd
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15870
diff
changeset
|
234 return ''.join(result) |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 def compile(pattern, flags=0): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 """compile(pattern[, flags]) -> RegexObject |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 Compile a regular expression pattern into a regular expression |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 object, which can be used for matching using its match() and |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 search() methods. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 groupindex={} |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 code=pcre_compile(pattern, flags, groupindex) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 return RegexObject(pattern, flags, code, groupindex) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
247 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 # |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 # Class definitions |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 # |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 class RegexObject: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 """Holds a compiled regular expression pattern. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 Methods: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 match Match the pattern to the beginning of a string. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 search Search a string for the presence of the pattern. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 sub Substitute occurrences of the pattern found in a string. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 subn Same as sub, but also return the number of substitutions made. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 split Split a string by the occurrences of the pattern. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 findall Find all occurrences of the pattern in a string. |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
263 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 def __init__(self, pattern, flags, code, groupindex): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
267 self.code = code |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 self.flags = flags |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 self.pattern = pattern |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 self.groupindex = groupindex |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 def search(self, string, pos=0, endpos=None): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 """search(string[, pos][, endpos]) -> MatchObject or None |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
274 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 Scan through string looking for a location where this regular |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 expression produces a match, and return a corresponding |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 MatchObject instance. Return None if no position in the string |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 matches the pattern; note that this is different from finding |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 a zero-length match at some point in the string. The optional |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 pos and endpos parameters have the same meaning as for the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 match() method. |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
282 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 """ |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
284 if endpos is None or endpos>len(string): |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 endpos=len(string) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 if endpos<pos: endpos=pos |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 regs = self.code.match(string, pos, endpos, 0) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 if regs is None: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 return None |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 self._num_regs=len(regs) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
291 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 return MatchObject(self, |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 string, |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 pos, endpos, |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 regs) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
296 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 def match(self, string, pos=0, endpos=None): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 """match(string[, pos][, endpos]) -> MatchObject or None |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
299 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 If zero or more characters at the beginning of string match |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 this regular expression, return a corresponding MatchObject |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 instance. Return None if the string does not match the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 pattern; note that this is different from a zero-length match. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 Note: If you want to locate a match anywhere in string, use |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 search() instead. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 The optional second parameter pos gives an index in the string |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 where the search is to start; it defaults to 0. This is not |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 completely equivalent to slicing the string; the '' pattern |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 character matches at the real beginning of the string and at |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 positions just after a newline, but not necessarily at the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 index where the search is to start. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 The optional parameter endpos limits how far the string will |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 be searched; it will be as if the string is endpos characters |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 long, so only the characters from pos to endpos will be |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 searched for a match. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 """ |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
321 if endpos is None or endpos>len(string): |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 endpos=len(string) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 if endpos<pos: endpos=pos |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 regs = self.code.match(string, pos, endpos, ANCHORED) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 if regs is None: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 return None |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 self._num_regs=len(regs) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 return MatchObject(self, |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 string, |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 pos, endpos, |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 regs) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
332 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 def sub(self, repl, string, count=0): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
334 """sub(repl, string[, count=0]) -> string |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
335 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 Return the string obtained by replacing the leftmost |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
337 non-overlapping occurrences of the compiled pattern in string |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
338 by the replacement repl. If the pattern isn't found, string is |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
339 returned unchanged. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
340 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
341 Identical to the sub() function, using the compiled pattern. |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
342 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 return self.subn(repl, string, count)[0] |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
345 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
346 def subn(self, repl, source, count=0): |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
347 """subn(repl, string[, count=0]) -> tuple |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
348 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 Perform the same operation as sub(), but return a tuple |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
350 (new_string, number_of_subs_made). |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
351 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
353 if count < 0: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
354 raise error, "negative substitution count" |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 if count == 0: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 count = sys.maxint |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
357 n = 0 # Number of matches |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
358 pos = 0 # Where to start searching |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
359 lastmatch = -1 # End of last match |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 results = [] # Substrings making up the result |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 end = len(source) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 if type(repl) is type(''): |
|
24997
a0dc6f504972
backport effbot's checkin of
Michael W. Hudson <mwh@python.net>
parents:
17799
diff
changeset
|
364 # See if repl contains group references (if it does, |
|
a0dc6f504972
backport effbot's checkin of
Michael W. Hudson <mwh@python.net>
parents:
17799
diff
changeset
|
365 # pcre_expand will attempt to call _Dummy.group, which |
|
a0dc6f504972
backport effbot's checkin of
Michael W. Hudson <mwh@python.net>
parents:
17799
diff
changeset
|
366 # results in a TypeError) |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
367 try: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
368 repl = pcre_expand(_Dummy, repl) |
|
24997
a0dc6f504972
backport effbot's checkin of
Michael W. Hudson <mwh@python.net>
parents:
17799
diff
changeset
|
369 except (error, TypeError): |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 m = MatchObject(self, source, 0, end, []) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 repl = lambda m, repl=repl, expand=pcre_expand: expand(m, repl) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 else: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 m = None |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
374 else: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
375 m = MatchObject(self, source, 0, end, []) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
377 match = self.code.match |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
378 append = results.append |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
379 while n < count and pos <= end: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
380 regs = match(source, pos, end, 0) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
381 if not regs: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 break |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
383 self._num_regs = len(regs) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
384 i, j = regs[0] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
385 if i == j == lastmatch: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
386 # Empty match adjacent to previous match |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
387 pos = pos + 1 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
388 append(source[lastmatch:pos]) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
389 continue |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
390 if pos < i: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
391 append(source[pos:i]) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
392 if m: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
393 m.pos = pos |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
394 m.regs = regs |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
395 append(repl(m)) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
396 else: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
397 append(repl) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
398 pos = lastmatch = j |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
399 if i == j: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
400 # Last match was empty; don't try here again |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
401 pos = pos + 1 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
402 append(source[lastmatch:pos]) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
403 n = n + 1 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
404 append(source[pos:]) |
|
16499
58de2829cfdd
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15870
diff
changeset
|
405 return (''.join(results), n) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
406 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
407 def split(self, source, maxsplit=0): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
408 """split(source[, maxsplit=0]) -> list of strings |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
409 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
410 Split string by the occurrences of the compiled pattern. If |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
411 capturing parentheses are used in the pattern, then the text |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
412 of all groups in the pattern are also returned as part of the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
413 resulting list. If maxsplit is nonzero, at most maxsplit |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 splits occur, and the remainder of the string is returned as |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
415 the final element of the list. |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
416 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
417 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
418 if maxsplit < 0: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
419 raise error, "negative split count" |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
420 if maxsplit == 0: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
421 maxsplit = sys.maxint |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
422 n = 0 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
423 pos = 0 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
424 lastmatch = 0 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
425 results = [] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 end = len(source) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
427 match = self.code.match |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
428 append = results.append |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
429 while n < maxsplit: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
430 regs = match(source, pos, end, 0) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
431 if not regs: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
432 break |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
433 i, j = regs[0] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
434 if i == j: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
435 # Empty match |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
436 if pos >= end: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
437 break |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
438 pos = pos+1 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
439 continue |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
440 append(source[lastmatch:i]) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
441 rest = regs[1:] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
442 if rest: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
443 for a, b in rest: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
444 if a == -1 or b == -1: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
445 group = None |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
446 else: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
447 group = source[a:b] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
448 append(group) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
449 pos = lastmatch = j |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
450 n = n + 1 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
451 append(source[lastmatch:]) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
452 return results |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
453 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
454 def findall(self, source): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
455 """findall(source) -> list |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
456 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
457 Return a list of all non-overlapping matches of the compiled |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
458 pattern in string. If one or more groups are present in the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
459 pattern, return a list of groups; this will be a list of |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
460 tuples if the pattern has more than one group. Empty matches |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
461 are included in the result. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
462 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
463 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
464 pos = 0 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
465 end = len(source) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
466 results = [] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
467 match = self.code.match |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
468 append = results.append |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
469 while pos <= end: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
470 regs = match(source, pos, end, 0) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
471 if not regs: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
472 break |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
473 i, j = regs[0] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
474 rest = regs[1:] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
475 if not rest: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
476 gr = source[i:j] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
477 elif len(rest) == 1: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
478 a, b = rest[0] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
479 gr = source[a:b] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
480 else: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
481 gr = [] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
482 for (a, b) in rest: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
483 gr.append(source[a:b]) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
484 gr = tuple(gr) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
485 append(gr) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
486 pos = max(j, pos+1) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
487 return results |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
488 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
489 # The following 3 functions were contributed by Mike Fletcher, and |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
490 # allow pickling and unpickling of RegexObject instances. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
491 def __getinitargs__(self): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
492 return (None,None,None,None) # any 4 elements, to work around |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
493 # problems with the |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
494 # pickle/cPickle modules not yet |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
495 # ignoring the __init__ function |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
496 def __getstate__(self): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
497 return self.pattern, self.flags, self.groupindex |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
498 def __setstate__(self, statetuple): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
499 self.pattern = statetuple[0] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
500 self.flags = statetuple[1] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
501 self.groupindex = statetuple[2] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
502 self.code = apply(pcre_compile, statetuple) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
503 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
504 class _Dummy: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
505 # Dummy class used by _subn_string(). Has 'group' to avoid core dump. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
506 group = None |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
507 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
508 class MatchObject: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
509 """Holds a compiled regular expression pattern. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
510 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
511 Methods: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
512 start Return the index of the start of a matched substring. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
513 end Return the index of the end of a matched substring. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
514 span Return a tuple of (start, end) of a matched substring. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
515 groups Return a tuple of all the subgroups of the match. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
516 group Return one or more subgroups of the match. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
517 groupdict Return a dictionary of all the named subgroups of the match. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
518 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
519 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
520 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
521 def __init__(self, re, string, pos, endpos, regs): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
522 self.re = re |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
523 self.string = string |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
524 self.pos = pos |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
525 self.endpos = endpos |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
526 self.regs = regs |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
527 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
528 def start(self, g = 0): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
529 """start([group=0]) -> int or None |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
530 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
531 Return the index of the start of the substring matched by |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
532 group; group defaults to zero (meaning the whole matched |
|
14396
a10afb3ad275
Correct docstring about return value when group didn't participate in match
Andrew M. Kuchling <amk@amk.ca>
parents:
13620
diff
changeset
|
533 substring). Return -1 if group exists but did not contribute |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
534 to the match. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
535 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
536 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
537 if type(g) == type(''): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
538 try: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
539 g = self.re.groupindex[g] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
540 except (KeyError, TypeError): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
541 raise IndexError, 'group %s is undefined' % `g` |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
542 return self.regs[g][0] |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
543 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
544 def end(self, g = 0): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
545 """end([group=0]) -> int or None |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
546 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
547 Return the indices of the end of the substring matched by |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
548 group; group defaults to zero (meaning the whole matched |
|
14396
a10afb3ad275
Correct docstring about return value when group didn't participate in match
Andrew M. Kuchling <amk@amk.ca>
parents:
13620
diff
changeset
|
549 substring). Return -1 if group exists but did not contribute |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
550 to the match. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
551 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
552 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
553 if type(g) == type(''): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
554 try: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
555 g = self.re.groupindex[g] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
556 except (KeyError, TypeError): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
557 raise IndexError, 'group %s is undefined' % `g` |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
558 return self.regs[g][1] |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
559 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
560 def span(self, g = 0): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
561 """span([group=0]) -> tuple |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
562 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
563 Return the 2-tuple (m.start(group), m.end(group)). Note that |
|
14396
a10afb3ad275
Correct docstring about return value when group didn't participate in match
Andrew M. Kuchling <amk@amk.ca>
parents:
13620
diff
changeset
|
564 if group did not contribute to the match, this is (-1, |
|
a10afb3ad275
Correct docstring about return value when group didn't participate in match
Andrew M. Kuchling <amk@amk.ca>
parents:
13620
diff
changeset
|
565 -1). Group defaults to zero (meaning the whole matched |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
566 substring). |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
567 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
568 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
569 if type(g) == type(''): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
570 try: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
571 g = self.re.groupindex[g] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
572 except (KeyError, TypeError): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
573 raise IndexError, 'group %s is undefined' % `g` |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
574 return self.regs[g] |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
575 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
576 def groups(self, default=None): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
577 """groups([default=None]) -> tuple |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
578 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
579 Return a tuple containing all the subgroups of the match, from |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
580 1 up to however many groups are in the pattern. The default |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
581 argument is used for groups that did not participate in the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
582 match. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
583 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
584 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
585 result = [] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
586 for g in range(1, self.re._num_regs): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
587 a, b = self.regs[g] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
588 if a == -1 or b == -1: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
589 result.append(default) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
590 else: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
591 result.append(self.string[a:b]) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
592 return tuple(result) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
593 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
594 def group(self, *groups): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
595 """group([group1, group2, ...]) -> string or tuple |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
596 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
597 Return one or more subgroups of the match. If there is a |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
598 single argument, the result is a single string; if there are |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
599 multiple arguments, the result is a tuple with one item per |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
600 argument. Without arguments, group1 defaults to zero (i.e. the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
601 whole match is returned). If a groupN argument is zero, the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
602 corresponding return value is the entire matching string; if |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
603 it is in the inclusive range [1..99], it is the string |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
604 matching the the corresponding parenthesized group. If a group |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
605 number is negative or larger than the number of groups defined |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
606 in the pattern, an IndexError exception is raised. If a group |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
607 is contained in a part of the pattern that did not match, the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
608 corresponding result is None. If a group is contained in a |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
609 part of the pattern that matched multiple times, the last |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
610 match is returned. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
611 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
612 If the regular expression uses the (?P<name>...) syntax, the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
613 groupN arguments may also be strings identifying groups by |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
614 their group name. If a string argument is not used as a group |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
615 name in the pattern, an IndexError exception is raised. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
616 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
617 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
618 if len(groups) == 0: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
619 groups = (0,) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
620 result = [] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
621 for g in groups: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
622 if type(g) == type(''): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
623 try: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
624 g = self.re.groupindex[g] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
625 except (KeyError, TypeError): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
626 raise IndexError, 'group %s is undefined' % `g` |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
627 if g >= len(self.regs): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
628 raise IndexError, 'group %s is undefined' % `g` |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
629 a, b = self.regs[g] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
630 if a == -1 or b == -1: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
631 result.append(None) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
632 else: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
633 result.append(self.string[a:b]) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
634 if len(result) > 1: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
635 return tuple(result) |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
636 elif len(result) == 1: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
637 return result[0] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
638 else: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
639 return () |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
640 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
641 def groupdict(self, default=None): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
642 """groupdict([default=None]) -> dictionary |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14396
diff
changeset
|
643 |
|
13229
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
644 Return a dictionary containing all the named subgroups of the |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
645 match, keyed by the subgroup name. The default argument is |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
646 used for groups that did not participate in the match. |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
647 |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
648 """ |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
649 dict = {} |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
650 for name, index in self.re.groupindex.items(): |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
651 a, b = self.regs[index] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
652 if a == -1 or b == -1: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
653 dict[name] = default |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
654 else: |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
655 dict[name] = self.string[a:b] |
|
344ab348f1e9
Switch to sre for regular expression matching (the new mini-re module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
656 return dict |
