| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
r"""Support for regular expressions (RE). |
| 17 |
|
| 18 |
This module provides regular expression matching operations similar to those |
| 19 |
found in Perl. It supports both 8-bit and Unicode strings; both the pattern and |
| 20 |
the strings being processed can contain null bytes and characters outside the |
| 21 |
US ASCII range. |
| 22 |
|
| 23 |
Regular expressions can contain both special and ordinary characters. Most |
| 24 |
ordinary characters, like "A", "a", or "0", are the simplest regular |
| 25 |
expressions; they simply match themselves. You can concatenate ordinary |
| 26 |
characters, so last matches the string 'last'. |
| 27 |
|
| 28 |
There are a few differences between the old (legacy) behaviour and the new |
| 29 |
(enhanced) behaviour, which are indicated by VERSION0 or VERSION1. |
| 30 |
|
| 31 |
The special characters are: |
| 32 |
"." Matches any character except a newline. |
| 33 |
"^" Matches the start of the string. |
| 34 |
"$" Matches the end of the string or just before the |
| 35 |
newline at the end of the string. |
| 36 |
"*" Matches 0 or more (greedy) repetitions of the preceding |
| 37 |
RE. Greedy means that it will match as many repetitions |
| 38 |
as possible. |
| 39 |
"+" Matches 1 or more (greedy) repetitions of the preceding |
| 40 |
RE. |
| 41 |
"?" Matches 0 or 1 (greedy) of the preceding RE. |
| 42 |
*?,+?,?? Non-greedy versions of the previous three special |
| 43 |
characters. |
| 44 |
*+,++,?+ Possessive versions of the previous three special |
| 45 |
characters. |
| 46 |
{m,n} Matches from m to n repetitions of the preceding RE. |
| 47 |
{m,n}? Non-greedy version of the above. |
| 48 |
{m,n}+ Possessive version of the above. |
| 49 |
{...} Fuzzy matching constraints. |
| 50 |
"\\" Either escapes special characters or signals a special |
| 51 |
sequence. |
| 52 |
[...] Indicates a set of characters. A "^" as the first |
| 53 |
character indicates a complementing set. |
| 54 |
"|" A|B, creates an RE that will match either A or B. |
| 55 |
(...) Matches the RE inside the parentheses. The contents are |
| 56 |
captured and can be retrieved or matched later in the |
| 57 |
string. |
| 58 |
(?flags-flags) VERSION1: Sets/clears the flags for the remainder of |
| 59 |
the group or pattern; VERSION0: Sets the flags for the |
| 60 |
entire pattern. |
| 61 |
(?:...) Non-capturing version of regular parentheses. |
| 62 |
(?>...) Atomic non-capturing version of regular parentheses. |
| 63 |
(?flags-flags:...) Non-capturing version of regular parentheses with local |
| 64 |
flags. |
| 65 |
(?P<name>...) The substring matched by the group is accessible by |
| 66 |
name. |
| 67 |
(?<name>...) The substring matched by the group is accessible by |
| 68 |
name. |
| 69 |
(?P=name) Matches the text matched earlier by the group named |
| 70 |
name. |
| 71 |
(?#...) A comment; ignored. |
| 72 |
(?=...) Matches if ... matches next, but doesn't consume the |
| 73 |
string. |
| 74 |
(?!...) Matches if ... doesn't match next. |
| 75 |
(?<=...) Matches if preceded by .... |
| 76 |
(?<!...) Matches if not preceded by .... |
| 77 |
(?(id)yes|no) Matches yes pattern if group id matched, the (optional) |
| 78 |
no pattern otherwise. |
| 79 |
(?(DEFINE)...) If there's no group called "DEFINE", then ... will be |
| 80 |
ignored, but any group definitions will be available. |
| 81 |
(?|...|...) (?|A|B), creates an RE that will match either A or B, |
| 82 |
but reuses capture group numbers across the |
| 83 |
alternatives. |
| 84 |
(*FAIL) Forces matching to fail, which means immediate |
| 85 |
backtracking. |
| 86 |
(*F) Abbreviation for (*FAIL). |
| 87 |
(*PRUNE) Discards the current backtracking information. Its |
| 88 |
effect doesn't extend outside an atomic group or a |
| 89 |
lookaround. |
| 90 |
(*SKIP) Similar to (*PRUNE), except that it also sets where in |
| 91 |
the text the next attempt at matching the entire |
| 92 |
pattern will start. Its effect doesn't extend outside |
| 93 |
an atomic group or a lookaround. |
| 94 |
|
| 95 |
The fuzzy matching constraints are: "i" to permit insertions, "d" to permit |
| 96 |
deletions, "s" to permit substitutions, "e" to permit any of these. Limits are |
| 97 |
optional with "<=" and "<". If any type of error is provided then any type not |
| 98 |
provided is not permitted. |
| 99 |
|
| 100 |
A cost equation may be provided. |
| 101 |
|
| 102 |
Examples: |
| 103 |
(?:fuzzy){i<=2} |
| 104 |
(?:fuzzy){i<=1,s<=2,d<=1,1i+1s+1d<3} |
| 105 |
|
| 106 |
VERSION1: Set operators are supported, and a set can include nested sets. The |
| 107 |
set operators, in order of increasing precedence, are: |
| 108 |
|| Set union ("x||y" means "x or y"). |
| 109 |
~~ (double tilde) Symmetric set difference ("x~~y" means "x or y, but not |
| 110 |
both"). |
| 111 |
&& Set intersection ("x&&y" means "x and y"). |
| 112 |
-- (double dash) Set difference ("x--y" means "x but not y"). |
| 113 |
|
| 114 |
Implicit union, ie, simple juxtaposition like in [ab], has the highest |
| 115 |
precedence. |
| 116 |
|
| 117 |
VERSION0 and VERSION1: |
| 118 |
The special sequences consist of "\\" and a character from the list below. If |
| 119 |
the ordinary character is not on the list, then the resulting RE will match the |
| 120 |
second character. |
| 121 |
\number Matches the contents of the group of the same number if |
| 122 |
number is no more than 2 digits, otherwise the character |
| 123 |
with the 3-digit octal code. |
| 124 |
\a Matches the bell character. |
| 125 |
\A Matches only at the start of the string. |
| 126 |
\b Matches the empty string, but only at the start or end of a |
| 127 |
word. |
| 128 |
\B Matches the empty string, but not at the start or end of a |
| 129 |
word. |
| 130 |
\d Matches any decimal digit; equivalent to the set [0-9] when |
| 131 |
matching a bytestring or a Unicode string with the ASCII |
| 132 |
flag, or the whole range of Unicode digits when matching a |
| 133 |
Unicode string. |
| 134 |
\D Matches any non-digit character; equivalent to [^\d]. |
| 135 |
\f Matches the formfeed character. |
| 136 |
\g<name> Matches the text matched by the group named name. |
| 137 |
\G Matches the empty string, but only at the position where |
| 138 |
the search started. |
| 139 |
\h Matches horizontal whitespace. |
| 140 |
\K Keeps only what follows for the entire match. |
| 141 |
\L<name> Named list. The list is provided as a keyword argument. |
| 142 |
\m Matches the empty string, but only at the start of a word. |
| 143 |
\M Matches the empty string, but only at the end of a word. |
| 144 |
\n Matches the newline character. |
| 145 |
\N{name} Matches the named character. |
| 146 |
\p{name=value} Matches the character if its property has the specified |
| 147 |
value. |
| 148 |
\P{name=value} Matches the character if its property hasn't the specified |
| 149 |
value. |
| 150 |
\r Matches the carriage-return character. |
| 151 |
\s Matches any whitespace character; equivalent to |
| 152 |
[ \t\n\r\f\v]. |
| 153 |
\S Matches any non-whitespace character; equivalent to [^\s]. |
| 154 |
\t Matches the tab character. |
| 155 |
\uXXXX Matches the Unicode codepoint with 4-digit hex code XXXX. |
| 156 |
\UXXXXXXXX Matches the Unicode codepoint with 8-digit hex code |
| 157 |
XXXXXXXX. |
| 158 |
\v Matches the vertical tab character. |
| 159 |
\w Matches any alphanumeric character; equivalent to |
| 160 |
[a-zA-Z0-9_] when matching a bytestring or a Unicode string |
| 161 |
with the ASCII flag, or the whole range of Unicode |
| 162 |
alphanumeric characters (letters plus digits plus |
| 163 |
underscore) when matching a Unicode string. With LOCALE, it |
| 164 |
will match the set [0-9_] plus characters defined as |
| 165 |
letters for the current locale. |
| 166 |
\W Matches the complement of \w; equivalent to [^\w]. |
| 167 |
\xXX Matches the character with 2-digit hex code XX. |
| 168 |
\X Matches a grapheme. |
| 169 |
\Z Matches only at the end of the string. |
| 170 |
\\ Matches a literal backslash. |
| 171 |
|
| 172 |
This module exports the following functions: |
| 173 |
match Match a regular expression pattern at the beginning of a string. |
| 174 |
fullmatch Match a regular expression pattern against all of a string. |
| 175 |
search Search a string for the presence of a pattern. |
| 176 |
sub Substitute occurrences of a pattern found in a string using a |
| 177 |
template string. |
| 178 |
subf Substitute occurrences of a pattern found in a string using a |
| 179 |
format string. |
| 180 |
subn Same as sub, but also return the number of substitutions made. |
| 181 |
subfn Same as subf, but also return the number of substitutions made. |
| 182 |
split Split a string by the occurrences of a pattern. VERSION1: will |
| 183 |
split at zero-width match; VERSION0: won't split at zero-width |
| 184 |
match. |
| 185 |
splititer Return an iterator yielding the parts of a split string. |
| 186 |
findall Find all occurrences of a pattern in a string. |
| 187 |
finditer Return an iterator yielding a match object for each match. |
| 188 |
compile Compile a pattern into a Pattern object. |
| 189 |
purge Clear the regular expression cache. |
| 190 |
escape Backslash all non-alphanumerics or special characters in a |
| 191 |
string. |
| 192 |
|
| 193 |
Most of the functions support a concurrent parameter: if True, the GIL will be |
| 194 |
released during matching, allowing other Python threads to run concurrently. If |
| 195 |
the string changes during matching, the behaviour is undefined. This parameter |
| 196 |
is not needed when working on the builtin (immutable) string classes. |
| 197 |
|
| 198 |
Some of the functions in this module take flags as optional parameters. Most of |
| 199 |
these flags can also be set within an RE: |
| 200 |
A a ASCII Make \w, \W, \b, \B, \d, and \D match the |
| 201 |
corresponding ASCII character categories. Default |
| 202 |
when matching a bytestring. |
| 203 |
B b BESTMATCH Find the best fuzzy match (default is first). |
| 204 |
D DEBUG Print the parsed pattern. |
| 205 |
E e ENHANCEMATCH Attempt to improve the fit after finding the first |
| 206 |
fuzzy match. |
| 207 |
F f FULLCASE Use full case-folding when performing |
| 208 |
case-insensitive matching in Unicode. |
| 209 |
I i IGNORECASE Perform case-insensitive matching. |
| 210 |
L L LOCALE Make \w, \W, \b, \B, \d, and \D dependent on the |
| 211 |
current locale. (One byte per character only.) |
| 212 |
M m MULTILINE "^" matches the beginning of lines (after a newline) |
| 213 |
as well as the string. "$" matches the end of lines |
| 214 |
(before a newline) as well as the end of the string. |
| 215 |
P p POSIX Perform POSIX-standard matching (leftmost longest). |
| 216 |
R r REVERSE Searches backwards. |
| 217 |
S s DOTALL "." matches any character at all, including the |
| 218 |
newline. |
| 219 |
U u UNICODE Make \w, \W, \b, \B, \d, and \D dependent on the |
| 220 |
Unicode locale. Default when matching a Unicode |
| 221 |
string. |
| 222 |
V0 V0 VERSION0 Turn on the old legacy behaviour. |
| 223 |
V1 V1 VERSION1 Turn on the new enhanced behaviour. This flag |
| 224 |
includes the FULLCASE flag. |
| 225 |
W w WORD Make \b and \B work with default Unicode word breaks |
| 226 |
and make ".", "^" and "$" work with Unicode line |
| 227 |
breaks. |
| 228 |
X x VERBOSE Ignore whitespace and comments for nicer looking REs. |
| 229 |
|
| 230 |
This module also defines an exception 'error'. |
| 231 |
|
| 232 |
""" |
| 233 |
|
| 234 |
|
| 235 |
__all__ = ["cache_all", "compile", "DEFAULT_VERSION", "escape", "findall", |
| 236 |
"finditer", "fullmatch", "match", "purge", "search", "split", "splititer", |
| 237 |
"sub", "subf", "subfn", "subn", "template", "Scanner", "A", "ASCII", "B", |
| 238 |
"BESTMATCH", "D", "DEBUG", "E", "ENHANCEMATCH", "S", "DOTALL", "F", |
| 239 |
"FULLCASE", "I", "IGNORECASE", "L", "LOCALE", "M", "MULTILINE", "P", "POSIX", |
| 240 |
"R", "REVERSE", "T", "TEMPLATE", "U", "UNICODE", "V0", "VERSION0", "V1", |
| 241 |
"VERSION1", "X", "VERBOSE", "W", "WORD", "error", "Regex", "__version__", |
| 242 |
"__doc__", "RegexFlag"] |
| 243 |
|
| 244 |
__version__ = "2.5.162" |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
def match(pattern, string, flags=0, pos=None, endpos=None, partial=False, |
| 250 |
concurrent=None, timeout=None, ignore_unused=False, **kwargs): |
| 251 |
"""Try to apply the pattern at the start of the string, returning a match |
| 252 |
object, or None if no match was found.""" |
| 253 |
pat = _compile(pattern, flags, ignore_unused, kwargs, True) |
| 254 |
return pat.match(string, pos, endpos, concurrent, partial, timeout) |
| 255 |
|
| 256 |
def fullmatch(pattern, string, flags=0, pos=None, endpos=None, partial=False, |
| 257 |
concurrent=None, timeout=None, ignore_unused=False, **kwargs): |
| 258 |
"""Try to apply the pattern against all of the string, returning a match |
| 259 |
object, or None if no match was found.""" |
| 260 |
pat = _compile(pattern, flags, ignore_unused, kwargs, True) |
| 261 |
return pat.fullmatch(string, pos, endpos, concurrent, partial, timeout) |
| 262 |
|
| 263 |
def search(pattern, string, flags=0, pos=None, endpos=None, partial=False, |
| 264 |
concurrent=None, timeout=None, ignore_unused=False, **kwargs): |
| 265 |
"""Search through string looking for a match to the pattern, returning a |
| 266 |
match object, or None if no match was found.""" |
| 267 |
pat = _compile(pattern, flags, ignore_unused, kwargs, True) |
| 268 |
return pat.search(string, pos, endpos, concurrent, partial, timeout) |
| 269 |
|
| 270 |
def sub(pattern, repl, string, count=0, flags=0, pos=None, endpos=None, |
| 271 |
concurrent=None, timeout=None, ignore_unused=False, **kwargs): |
| 272 |
"""Return the string obtained by replacing the leftmost (or rightmost with a |
| 273 |
reverse pattern) non-overlapping occurrences of the pattern in string by the |
| 274 |
replacement repl. repl can be either a string or a callable; if a string, |
| 275 |
backslash escapes in it are processed; if a callable, it's passed the match |
| 276 |
object and must return a replacement string to be used.""" |
| 277 |
pat = _compile(pattern, flags, ignore_unused, kwargs, True) |
| 278 |
return pat.sub(repl, string, count, pos, endpos, concurrent, timeout) |
| 279 |
|
| 280 |
def subf(pattern, format, string, count=0, flags=0, pos=None, endpos=None, |
| 281 |
concurrent=None, timeout=None, ignore_unused=False, **kwargs): |
| 282 |
"""Return the string obtained by replacing the leftmost (or rightmost with a |
| 283 |
reverse pattern) non-overlapping occurrences of the pattern in string by the |
| 284 |
replacement format. format can be either a string or a callable; if a string, |
| 285 |
it's treated as a format string; if a callable, it's passed the match object |
| 286 |
and must return a replacement string to be used.""" |
| 287 |
pat = _compile(pattern, flags, ignore_unused, kwargs, True) |
| 288 |
return pat.subf(format, string, count, pos, endpos, concurrent, timeout) |
| 289 |
|
| 290 |
def subn(pattern, repl, string, count=0, flags=0, pos=None, endpos=None, |
| 291 |
concurrent=None, timeout=None, ignore_unused=False, **kwargs): |
| 292 |
"""Return a 2-tuple containing (new_string, number). new_string is the string |
| 293 |
obtained by replacing the leftmost (or rightmost with a reverse pattern) |
| 294 |
non-overlapping occurrences of the pattern in the source string by the |
| 295 |
replacement repl. number is the number of substitutions that were made. repl |
| 296 |
can be either a string or a callable; if a string, backslash escapes in it |
| 297 |
are processed; if a callable, it's passed the match object and must return a |
| 298 |
replacement string to be used.""" |
| 299 |
pat = _compile(pattern, flags, ignore_unused, kwargs, True) |
| 300 |
return pat.subn(repl, string, count, pos, endpos, concurrent, timeout) |
| 301 |
|
| 302 |
def subfn(pattern, format, string, count=0, flags=0, pos=None, endpos=None, |
| 303 |
concurrent=None, timeout=None, ignore_unused=False, **kwargs): |
| 304 |
"""Return a 2-tuple containing (new_string, number). new_string is the string |
| 305 |
obtained by replacing the leftmost (or rightmost with a reverse pattern) |
| 306 |
non-overlapping occurrences of the pattern in the source string by the |
| 307 |
replacement format. number is the number of substitutions that were made. format |
| 308 |
can be either a string or a callable; if a string, it's treated as a format |
| 309 |
string; if a callable, it's passed the match object and must return a |
| 310 |
replacement string to be used.""" |
| 311 |
pat = _compile(pattern, flags, ignore_unused, kwargs, True) |
| 312 |
return pat.subfn(format, string, count, pos, endpos, concurrent, timeout) |
| 313 |
|
| 314 |
def split(pattern, string, maxsplit=0, flags=0, concurrent=None, timeout=None, |
| 315 |
ignore_unused=False, **kwargs): |
| 316 |
"""Split the source string by the occurrences of the pattern, returning a |
| 317 |
list containing the resulting substrings. If capturing parentheses are used |
| 318 |
in pattern, then the text of all groups in the pattern are also returned as |
| 319 |
part of the resulting list. If maxsplit is nonzero, at most maxsplit splits |
| 320 |
occur, and the remainder of the string is returned as the final element of |
| 321 |
the list.""" |
| 322 |
pat = _compile(pattern, flags, ignore_unused, kwargs, True) |
| 323 |
return pat.split(string, maxsplit, concurrent, timeout) |
| 324 |
|
| 325 |
def splititer(pattern, string, maxsplit=0, flags=0, concurrent=None, |
| 326 |
timeout=None, ignore_unused=False, **kwargs): |
| 327 |
"Return an iterator yielding the parts of a split string." |
| 328 |
pat = _compile(pattern, flags, ignore_unused, kwargs, True) |
| 329 |
return pat.splititer(string, maxsplit, concurrent, timeout) |
| 330 |
|
| 331 |
def findall(pattern, string, flags=0, pos=None, endpos=None, overlapped=False, |
| 332 |
concurrent=None, timeout=None, ignore_unused=False, **kwargs): |
| 333 |
"""Return a list of all matches in the string. The matches may be overlapped |
| 334 |
if overlapped is True. If one or more groups are present in the pattern, |
| 335 |
return a list of groups; this will be a list of tuples if the pattern has |
| 336 |
more than one group. Empty matches are included in the result.""" |
| 337 |
pat = _compile(pattern, flags, ignore_unused, kwargs, True) |
| 338 |
return pat.findall(string, pos, endpos, overlapped, concurrent, timeout) |
| 339 |
|
| 340 |
def finditer(pattern, string, flags=0, pos=None, endpos=None, overlapped=False, |
| 341 |
partial=False, concurrent=None, timeout=None, ignore_unused=False, **kwargs): |
| 342 |
"""Return an iterator over all matches in the string. The matches may be |
| 343 |
overlapped if overlapped is True. For each match, the iterator returns a |
| 344 |
match object. Empty matches are included in the result.""" |
| 345 |
pat = _compile(pattern, flags, ignore_unused, kwargs, True) |
| 346 |
return pat.finditer(string, pos, endpos, overlapped, concurrent, partial, |
| 347 |
timeout) |
| 348 |
|
| 349 |
def compile(pattern, flags=0, ignore_unused=False, cache_pattern=None, **kwargs): |
| 350 |
"Compile a regular expression pattern, returning a pattern object." |
| 351 |
if cache_pattern is None: |
| 352 |
cache_pattern = _cache_all |
| 353 |
return _compile(pattern, flags, ignore_unused, kwargs, cache_pattern) |
| 354 |
|
| 355 |
def purge(): |
| 356 |
"Clear the regular expression cache" |
| 357 |
_cache.clear() |
| 358 |
_locale_sensitive.clear() |
| 359 |
|
| 360 |
|
| 361 |
_cache_all = True |
| 362 |
|
| 363 |
def cache_all(value=True): |
| 364 |
"""Sets whether to cache all patterns, even those are compiled explicitly. |
| 365 |
Passing None has no effect, but returns the current setting.""" |
| 366 |
global _cache_all |
| 367 |
|
| 368 |
if value is None: |
| 369 |
return _cache_all |
| 370 |
|
| 371 |
_cache_all = value |
| 372 |
|
| 373 |
def template(pattern, flags=0): |
| 374 |
"Compile a template pattern, returning a pattern object." |
| 375 |
return _compile(pattern, flags | TEMPLATE, False, {}, False) |
| 376 |
|
| 377 |
def escape(pattern, special_only=True, literal_spaces=False): |
| 378 |
"""Escape a string for use as a literal in a pattern. If special_only is |
| 379 |
True, escape only special characters, else escape all non-alphanumeric |
| 380 |
characters. If literal_spaces is True, don't escape spaces.""" |
| 381 |
|
| 382 |
if isinstance(pattern, bytes): |
| 383 |
p = pattern.decode("latin-1") |
| 384 |
else: |
| 385 |
p = pattern |
| 386 |
|
| 387 |
s = [] |
| 388 |
if special_only: |
| 389 |
for c in p: |
| 390 |
if c == " " and literal_spaces: |
| 391 |
s.append(c) |
| 392 |
elif c in _METACHARS or c.isspace(): |
| 393 |
s.append("\\") |
| 394 |
s.append(c) |
| 395 |
else: |
| 396 |
s.append(c) |
| 397 |
else: |
| 398 |
for c in p: |
| 399 |
if c == " " and literal_spaces: |
| 400 |
s.append(c) |
| 401 |
elif c in _ALNUM: |
| 402 |
s.append(c) |
| 403 |
else: |
| 404 |
s.append("\\") |
| 405 |
s.append(c) |
| 406 |
|
| 407 |
r = "".join(s) |
| 408 |
|
| 409 |
if isinstance(pattern, bytes): |
| 410 |
r = r.encode("latin-1") |
| 411 |
|
| 412 |
return r |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
import regex._regex_core as _regex_core |
| 418 |
import regex._regex as _regex |
| 419 |
from threading import RLock as _RLock |
| 420 |
from locale import getpreferredencoding as _getpreferredencoding |
| 421 |
from regex._regex_core import * |
| 422 |
from regex._regex_core import (_ALL_VERSIONS, _ALL_ENCODINGS, _FirstSetError, |
| 423 |
_UnscopedFlagSet, _check_group_features, _compile_firstset, |
| 424 |
_compile_replacement, _flatten_code, _fold_case, _get_required_string, |
| 425 |
_parse_pattern, _shrink_cache) |
| 426 |
from regex._regex_core import (ALNUM as _ALNUM, Info as _Info, OP as _OP, Source |
| 427 |
as _Source, Fuzzy as _Fuzzy) |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
DEFAULT_VERSION = VERSION0 |
| 433 |
|
| 434 |
_METACHARS = frozenset("()[]{}?*+|^$\\.-#&~") |
| 435 |
|
| 436 |
_regex_core.DEFAULT_VERSION = DEFAULT_VERSION |
| 437 |
|
| 438 |
|
| 439 |
_cache = {} |
| 440 |
_cache_lock = _RLock() |
| 441 |
_named_args = {} |
| 442 |
_replacement_cache = {} |
| 443 |
_locale_sensitive = {} |
| 444 |
|
| 445 |
|
| 446 |
_MAXCACHE = 500 |
| 447 |
_MAXREPCACHE = 500 |
| 448 |
|
| 449 |
def _compile(pattern, flags, ignore_unused, kwargs, cache_it): |
| 450 |
"Compiles a regular expression to a PatternObject." |
| 451 |
|
| 452 |
global DEFAULT_VERSION |
| 453 |
try: |
| 454 |
from regex import DEFAULT_VERSION |
| 455 |
except ImportError: |
| 456 |
pass |
| 457 |
|
| 458 |
|
| 459 |
if (flags & DEBUG) != 0: |
| 460 |
cache_it = False |
| 461 |
|
| 462 |
|
| 463 |
locale_key = (type(pattern), pattern) |
| 464 |
if _locale_sensitive.get(locale_key, True) or (flags & LOCALE) != 0: |
| 465 |
|
| 466 |
pattern_locale = _getpreferredencoding() |
| 467 |
else: |
| 468 |
|
| 469 |
pattern_locale = None |
| 470 |
|
| 471 |
def complain_unused_args(): |
| 472 |
if ignore_unused: |
| 473 |
return |
| 474 |
|
| 475 |
|
| 476 |
unused_kwargs = set(kwargs) - {k for k, v in args_needed} |
| 477 |
if unused_kwargs: |
| 478 |
any_one = next(iter(unused_kwargs)) |
| 479 |
raise ValueError('unused keyword argument {!a}'.format(any_one)) |
| 480 |
|
| 481 |
if cache_it: |
| 482 |
try: |
| 483 |
|
| 484 |
args_key = pattern, type(pattern), flags |
| 485 |
args_needed = _named_args[args_key] |
| 486 |
|
| 487 |
|
| 488 |
args_supplied = set() |
| 489 |
if args_needed: |
| 490 |
for k, v in args_needed: |
| 491 |
try: |
| 492 |
args_supplied.add((k, frozenset(kwargs[k]))) |
| 493 |
except KeyError: |
| 494 |
raise error("missing named list: {!r}".format(k)) |
| 495 |
|
| 496 |
complain_unused_args() |
| 497 |
|
| 498 |
args_supplied = frozenset(args_supplied) |
| 499 |
|
| 500 |
|
| 501 |
pattern_key = (pattern, type(pattern), flags, args_supplied, |
| 502 |
DEFAULT_VERSION, pattern_locale) |
| 503 |
return _cache[pattern_key] |
| 504 |
except KeyError: |
| 505 |
|
| 506 |
pass |
| 507 |
|
| 508 |
|
| 509 |
if isinstance(pattern, str): |
| 510 |
guess_encoding = UNICODE |
| 511 |
elif isinstance(pattern, bytes): |
| 512 |
guess_encoding = ASCII |
| 513 |
elif isinstance(pattern, Pattern): |
| 514 |
if flags: |
| 515 |
raise ValueError("cannot process flags argument with a compiled pattern") |
| 516 |
|
| 517 |
return pattern |
| 518 |
else: |
| 519 |
raise TypeError("first argument must be a string or compiled pattern") |
| 520 |
|
| 521 |
|
| 522 |
_regex_core.DEFAULT_VERSION = DEFAULT_VERSION |
| 523 |
|
| 524 |
global_flags = flags |
| 525 |
|
| 526 |
while True: |
| 527 |
caught_exception = None |
| 528 |
try: |
| 529 |
source = _Source(pattern) |
| 530 |
info = _Info(global_flags, source.char_type, kwargs) |
| 531 |
info.guess_encoding = guess_encoding |
| 532 |
source.ignore_space = bool(info.flags & VERBOSE) |
| 533 |
parsed = _parse_pattern(source, info) |
| 534 |
break |
| 535 |
except _UnscopedFlagSet: |
| 536 |
|
| 537 |
global_flags = info.global_flags |
| 538 |
except error as e: |
| 539 |
caught_exception = e |
| 540 |
|
| 541 |
if caught_exception: |
| 542 |
raise error(caught_exception.msg, caught_exception.pattern, |
| 543 |
caught_exception.pos) |
| 544 |
|
| 545 |
if not source.at_end(): |
| 546 |
raise error("unbalanced parenthesis", pattern, source.pos) |
| 547 |
|
| 548 |
|
| 549 |
version = (info.flags & _ALL_VERSIONS) or DEFAULT_VERSION |
| 550 |
if version not in (0, VERSION0, VERSION1): |
| 551 |
raise ValueError("VERSION0 and VERSION1 flags are mutually incompatible") |
| 552 |
|
| 553 |
if (info.flags & _ALL_ENCODINGS) not in (0, ASCII, LOCALE, UNICODE): |
| 554 |
raise ValueError("ASCII, LOCALE and UNICODE flags are mutually incompatible") |
| 555 |
|
| 556 |
if isinstance(pattern, bytes) and (info.flags & UNICODE): |
| 557 |
raise ValueError("cannot use UNICODE flag with a bytes pattern") |
| 558 |
|
| 559 |
if not (info.flags & _ALL_ENCODINGS): |
| 560 |
if isinstance(pattern, str): |
| 561 |
info.flags |= UNICODE |
| 562 |
else: |
| 563 |
info.flags |= ASCII |
| 564 |
|
| 565 |
reverse = bool(info.flags & REVERSE) |
| 566 |
fuzzy = isinstance(parsed, _Fuzzy) |
| 567 |
|
| 568 |
|
| 569 |
_locale_sensitive[locale_key] = info.inline_locale |
| 570 |
|
| 571 |
|
| 572 |
caught_exception = None |
| 573 |
try: |
| 574 |
parsed.fix_groups(pattern, reverse, False) |
| 575 |
except error as e: |
| 576 |
caught_exception = e |
| 577 |
|
| 578 |
if caught_exception: |
| 579 |
raise error(caught_exception.msg, caught_exception.pattern, |
| 580 |
caught_exception.pos) |
| 581 |
|
| 582 |
|
| 583 |
if flags & DEBUG: |
| 584 |
parsed.dump(indent=0, reverse=reverse) |
| 585 |
|
| 586 |
|
| 587 |
parsed = parsed.optimise(info, reverse) |
| 588 |
parsed = parsed.pack_characters(info) |
| 589 |
|
| 590 |
|
| 591 |
req_offset, req_chars, req_flags = _get_required_string(parsed, info.flags) |
| 592 |
|
| 593 |
|
| 594 |
named_lists = {} |
| 595 |
named_list_indexes = [None] * len(info.named_lists_used) |
| 596 |
args_needed = set() |
| 597 |
for key, index in info.named_lists_used.items(): |
| 598 |
name, case_flags = key |
| 599 |
values = frozenset(kwargs[name]) |
| 600 |
if case_flags: |
| 601 |
items = frozenset(_fold_case(info, v) for v in values) |
| 602 |
else: |
| 603 |
items = values |
| 604 |
named_lists[name] = values |
| 605 |
named_list_indexes[index] = items |
| 606 |
args_needed.add((name, values)) |
| 607 |
|
| 608 |
complain_unused_args() |
| 609 |
|
| 610 |
|
| 611 |
_check_group_features(info, parsed) |
| 612 |
|
| 613 |
|
| 614 |
code = parsed.compile(reverse) |
| 615 |
|
| 616 |
|
| 617 |
key = (0, reverse, fuzzy) |
| 618 |
ref = info.call_refs.get(key) |
| 619 |
if ref is not None: |
| 620 |
code = [(_OP.CALL_REF, ref)] + code + [(_OP.END, )] |
| 621 |
|
| 622 |
|
| 623 |
code += [(_OP.SUCCESS, )] |
| 624 |
|
| 625 |
|
| 626 |
for group, rev, fuz in info.additional_groups: |
| 627 |
code += group.compile(rev, fuz) |
| 628 |
|
| 629 |
|
| 630 |
code = _flatten_code(code) |
| 631 |
|
| 632 |
if not parsed.has_simple_start(): |
| 633 |
|
| 634 |
try: |
| 635 |
fs_code = _compile_firstset(info, parsed.get_firstset(reverse)) |
| 636 |
fs_code = _flatten_code(fs_code) |
| 637 |
code = fs_code + code |
| 638 |
except _FirstSetError: |
| 639 |
pass |
| 640 |
|
| 641 |
|
| 642 |
index_group = dict((v, n) for n, v in info.group_index.items()) |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
compiled_pattern = _regex.compile(pattern, info.flags | version, code, |
| 650 |
info.group_index, index_group, named_lists, named_list_indexes, |
| 651 |
req_offset, req_chars, req_flags, info.group_count) |
| 652 |
|
| 653 |
|
| 654 |
if len(_cache) >= _MAXCACHE: |
| 655 |
with _cache_lock: |
| 656 |
_shrink_cache(_cache, _named_args, _locale_sensitive, _MAXCACHE) |
| 657 |
|
| 658 |
if cache_it: |
| 659 |
if (info.flags & LOCALE) == 0: |
| 660 |
pattern_locale = None |
| 661 |
|
| 662 |
args_needed = frozenset(args_needed) |
| 663 |
|
| 664 |
|
| 665 |
pattern_key = (pattern, type(pattern), flags, args_needed, |
| 666 |
DEFAULT_VERSION, pattern_locale) |
| 667 |
_cache[pattern_key] = compiled_pattern |
| 668 |
|
| 669 |
|
| 670 |
_named_args[args_key] = args_needed |
| 671 |
|
| 672 |
return compiled_pattern |
| 673 |
|
| 674 |
def _compile_replacement_helper(pattern, template): |
| 675 |
"Compiles a replacement template." |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
key = pattern.pattern, pattern.flags, template |
| 680 |
compiled = _replacement_cache.get(key) |
| 681 |
if compiled is not None: |
| 682 |
return compiled |
| 683 |
|
| 684 |
if len(_replacement_cache) >= _MAXREPCACHE: |
| 685 |
_replacement_cache.clear() |
| 686 |
|
| 687 |
is_unicode = isinstance(template, str) |
| 688 |
source = _Source(template) |
| 689 |
if is_unicode: |
| 690 |
def make_string(char_codes): |
| 691 |
return "".join(chr(c) for c in char_codes) |
| 692 |
else: |
| 693 |
def make_string(char_codes): |
| 694 |
return bytes(char_codes) |
| 695 |
|
| 696 |
compiled = [] |
| 697 |
literal = [] |
| 698 |
while True: |
| 699 |
ch = source.get() |
| 700 |
if not ch: |
| 701 |
break |
| 702 |
if ch == "\\": |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
is_group, items = _compile_replacement(source, pattern, is_unicode) |
| 707 |
if is_group: |
| 708 |
|
| 709 |
if literal: |
| 710 |
compiled.append(make_string(literal)) |
| 711 |
literal = [] |
| 712 |
compiled.extend(items) |
| 713 |
else: |
| 714 |
literal.extend(items) |
| 715 |
else: |
| 716 |
literal.append(ord(ch)) |
| 717 |
|
| 718 |
|
| 719 |
if literal: |
| 720 |
compiled.append(make_string(literal)) |
| 721 |
|
| 722 |
_replacement_cache[key] = compiled |
| 723 |
|
| 724 |
return compiled |
| 725 |
|
| 726 |
|
| 727 |
_pat = _compile('', 0, False, {}, False) |
| 728 |
Pattern = type(_pat) |
| 729 |
Match = type(_pat.match('')) |
| 730 |
del _pat |
| 731 |
|
| 732 |
|
| 733 |
__all__.append("Pattern") |
| 734 |
__all__.append("Match") |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
Regex = compile |
| 739 |
|
| 740 |
|
| 741 |
import copyreg as _copy_reg |
| 742 |
|
| 743 |
def _pickle(pattern): |
| 744 |
return _regex.compile, pattern._pickled_data |
| 745 |
|
| 746 |
_copy_reg.pickle(Pattern, _pickle) |