Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python3....
File: re.py
#
[0] Fix | Delete
# Secret Labs' Regular Expression Engine
[1] Fix | Delete
#
[2] Fix | Delete
# re-compatible interface for the sre matching engine
[3] Fix | Delete
#
[4] Fix | Delete
# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.
[5] Fix | Delete
#
[6] Fix | Delete
# This version of the SRE library can be redistributed under CNRI's
[7] Fix | Delete
# Python 1.6 license. For any other use, please contact Secret Labs
[8] Fix | Delete
# AB (info@pythonware.com).
[9] Fix | Delete
#
[10] Fix | Delete
# Portions of this engine have been developed in cooperation with
[11] Fix | Delete
# CNRI. Hewlett-Packard provided funding for 1.6 integration and
[12] Fix | Delete
# other compatibility work.
[13] Fix | Delete
#
[14] Fix | Delete
[15] Fix | Delete
r"""Support for regular expressions (RE).
[16] Fix | Delete
[17] Fix | Delete
This module provides regular expression matching operations similar to
[18] Fix | Delete
those found in Perl. It supports both 8-bit and Unicode strings; both
[19] Fix | Delete
the pattern and the strings being processed can contain null bytes and
[20] Fix | Delete
characters outside the US ASCII range.
[21] Fix | Delete
[22] Fix | Delete
Regular expressions can contain both special and ordinary characters.
[23] Fix | Delete
Most ordinary characters, like "A", "a", or "0", are the simplest
[24] Fix | Delete
regular expressions; they simply match themselves. You can
[25] Fix | Delete
concatenate ordinary characters, so last matches the string 'last'.
[26] Fix | Delete
[27] Fix | Delete
The special characters are:
[28] Fix | Delete
"." Matches any character except a newline.
[29] Fix | Delete
"^" Matches the start of the string.
[30] Fix | Delete
"$" Matches the end of the string or just before the newline at
[31] Fix | Delete
the end of the string.
[32] Fix | Delete
"*" Matches 0 or more (greedy) repetitions of the preceding RE.
[33] Fix | Delete
Greedy means that it will match as many repetitions as possible.
[34] Fix | Delete
"+" Matches 1 or more (greedy) repetitions of the preceding RE.
[35] Fix | Delete
"?" Matches 0 or 1 (greedy) of the preceding RE.
[36] Fix | Delete
*?,+?,?? Non-greedy versions of the previous three special characters.
[37] Fix | Delete
{m,n} Matches from m to n repetitions of the preceding RE.
[38] Fix | Delete
{m,n}? Non-greedy version of the above.
[39] Fix | Delete
"\\" Either escapes special characters or signals a special sequence.
[40] Fix | Delete
[] Indicates a set of characters.
[41] Fix | Delete
A "^" as the first character indicates a complementing set.
[42] Fix | Delete
"|" A|B, creates an RE that will match either A or B.
[43] Fix | Delete
(...) Matches the RE inside the parentheses.
[44] Fix | Delete
The contents can be retrieved or matched later in the string.
[45] Fix | Delete
(?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).
[46] Fix | Delete
(?:...) Non-grouping version of regular parentheses.
[47] Fix | Delete
(?P<name>...) The substring matched by the group is accessible by name.
[48] Fix | Delete
(?P=name) Matches the text matched earlier by the group named name.
[49] Fix | Delete
(?#...) A comment; ignored.
[50] Fix | Delete
(?=...) Matches if ... matches next, but doesn't consume the string.
[51] Fix | Delete
(?!...) Matches if ... doesn't match next.
[52] Fix | Delete
(?<=...) Matches if preceded by ... (must be fixed length).
[53] Fix | Delete
(?<!...) Matches if not preceded by ... (must be fixed length).
[54] Fix | Delete
(?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
[55] Fix | Delete
the (optional) no pattern otherwise.
[56] Fix | Delete
[57] Fix | Delete
The special sequences consist of "\\" and a character from the list
[58] Fix | Delete
below. If the ordinary character is not on the list, then the
[59] Fix | Delete
resulting RE will match the second character.
[60] Fix | Delete
\number Matches the contents of the group of the same number.
[61] Fix | Delete
\A Matches only at the start of the string.
[62] Fix | Delete
\Z Matches only at the end of the string.
[63] Fix | Delete
\b Matches the empty string, but only at the start or end of a word.
[64] Fix | Delete
\B Matches the empty string, but not at the start or end of a word.
[65] Fix | Delete
\d Matches any decimal digit; equivalent to the set [0-9] in
[66] Fix | Delete
bytes patterns or string patterns with the ASCII flag.
[67] Fix | Delete
In string patterns without the ASCII flag, it will match the whole
[68] Fix | Delete
range of Unicode digits.
[69] Fix | Delete
\D Matches any non-digit character; equivalent to [^\d].
[70] Fix | Delete
\s Matches any whitespace character; equivalent to [ \t\n\r\f\v] in
[71] Fix | Delete
bytes patterns or string patterns with the ASCII flag.
[72] Fix | Delete
In string patterns without the ASCII flag, it will match the whole
[73] Fix | Delete
range of Unicode whitespace characters.
[74] Fix | Delete
\S Matches any non-whitespace character; equivalent to [^\s].
[75] Fix | Delete
\w Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]
[76] Fix | Delete
in bytes patterns or string patterns with the ASCII flag.
[77] Fix | Delete
In string patterns without the ASCII flag, it will match the
[78] Fix | Delete
range of Unicode alphanumeric characters (letters plus digits
[79] Fix | Delete
plus underscore).
[80] Fix | Delete
With LOCALE, it will match the set [0-9_] plus characters defined
[81] Fix | Delete
as letters for the current locale.
[82] Fix | Delete
\W Matches the complement of \w.
[83] Fix | Delete
\\ Matches a literal backslash.
[84] Fix | Delete
[85] Fix | Delete
This module exports the following functions:
[86] Fix | Delete
match Match a regular expression pattern to the beginning of a string.
[87] Fix | Delete
fullmatch Match a regular expression pattern to all of a string.
[88] Fix | Delete
search Search a string for the presence of a pattern.
[89] Fix | Delete
sub Substitute occurrences of a pattern found in a string.
[90] Fix | Delete
subn Same as sub, but also return the number of substitutions made.
[91] Fix | Delete
split Split a string by the occurrences of a pattern.
[92] Fix | Delete
findall Find all occurrences of a pattern in a string.
[93] Fix | Delete
finditer Return an iterator yielding a match object for each match.
[94] Fix | Delete
compile Compile a pattern into a RegexObject.
[95] Fix | Delete
purge Clear the regular expression cache.
[96] Fix | Delete
escape Backslash all non-alphanumerics in a string.
[97] Fix | Delete
[98] Fix | Delete
Some of the functions in this module takes flags as optional parameters:
[99] Fix | Delete
A ASCII For string patterns, make \w, \W, \b, \B, \d, \D
[100] Fix | Delete
match the corresponding ASCII character categories
[101] Fix | Delete
(rather than the whole Unicode categories, which is the
[102] Fix | Delete
default).
[103] Fix | Delete
For bytes patterns, this flag is the only available
[104] Fix | Delete
behaviour and needn't be specified.
[105] Fix | Delete
I IGNORECASE Perform case-insensitive matching.
[106] Fix | Delete
L LOCALE Make \w, \W, \b, \B, dependent on the current locale.
[107] Fix | Delete
M MULTILINE "^" matches the beginning of lines (after a newline)
[108] Fix | Delete
as well as the string.
[109] Fix | Delete
"$" matches the end of lines (before a newline) as well
[110] Fix | Delete
as the end of the string.
[111] Fix | Delete
S DOTALL "." matches any character at all, including the newline.
[112] Fix | Delete
X VERBOSE Ignore whitespace and comments for nicer looking RE's.
[113] Fix | Delete
U UNICODE For compatibility only. Ignored for string patterns (it
[114] Fix | Delete
is the default), and forbidden for bytes patterns.
[115] Fix | Delete
[116] Fix | Delete
This module also defines an exception 'error'.
[117] Fix | Delete
[118] Fix | Delete
"""
[119] Fix | Delete
[120] Fix | Delete
import enum
[121] Fix | Delete
import sre_compile
[122] Fix | Delete
import sre_parse
[123] Fix | Delete
import functools
[124] Fix | Delete
try:
[125] Fix | Delete
import _locale
[126] Fix | Delete
except ImportError:
[127] Fix | Delete
_locale = None
[128] Fix | Delete
[129] Fix | Delete
# public symbols
[130] Fix | Delete
__all__ = [
[131] Fix | Delete
"match", "fullmatch", "search", "sub", "subn", "split",
[132] Fix | Delete
"findall", "finditer", "compile", "purge", "template", "escape",
[133] Fix | Delete
"error", "A", "I", "L", "M", "S", "X", "U",
[134] Fix | Delete
"ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
[135] Fix | Delete
"UNICODE",
[136] Fix | Delete
]
[137] Fix | Delete
[138] Fix | Delete
__version__ = "2.2.1"
[139] Fix | Delete
[140] Fix | Delete
class RegexFlag(enum.IntFlag):
[141] Fix | Delete
ASCII = sre_compile.SRE_FLAG_ASCII # assume ascii "locale"
[142] Fix | Delete
IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case
[143] Fix | Delete
LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
[144] Fix | Delete
UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode "locale"
[145] Fix | Delete
MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline
[146] Fix | Delete
DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline
[147] Fix | Delete
VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments
[148] Fix | Delete
A = ASCII
[149] Fix | Delete
I = IGNORECASE
[150] Fix | Delete
L = LOCALE
[151] Fix | Delete
U = UNICODE
[152] Fix | Delete
M = MULTILINE
[153] Fix | Delete
S = DOTALL
[154] Fix | Delete
X = VERBOSE
[155] Fix | Delete
# sre extensions (experimental, don't rely on these)
[156] Fix | Delete
TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
[157] Fix | Delete
T = TEMPLATE
[158] Fix | Delete
DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
[159] Fix | Delete
globals().update(RegexFlag.__members__)
[160] Fix | Delete
[161] Fix | Delete
# sre exception
[162] Fix | Delete
error = sre_compile.error
[163] Fix | Delete
[164] Fix | Delete
# --------------------------------------------------------------------
[165] Fix | Delete
# public interface
[166] Fix | Delete
[167] Fix | Delete
def match(pattern, string, flags=0):
[168] Fix | Delete
"""Try to apply the pattern at the start of the string, returning
[169] Fix | Delete
a match object, or None if no match was found."""
[170] Fix | Delete
return _compile(pattern, flags).match(string)
[171] Fix | Delete
[172] Fix | Delete
def fullmatch(pattern, string, flags=0):
[173] Fix | Delete
"""Try to apply the pattern to all of the string, returning
[174] Fix | Delete
a match object, or None if no match was found."""
[175] Fix | Delete
return _compile(pattern, flags).fullmatch(string)
[176] Fix | Delete
[177] Fix | Delete
def search(pattern, string, flags=0):
[178] Fix | Delete
"""Scan through string looking for a match to the pattern, returning
[179] Fix | Delete
a match object, or None if no match was found."""
[180] Fix | Delete
return _compile(pattern, flags).search(string)
[181] Fix | Delete
[182] Fix | Delete
def sub(pattern, repl, string, count=0, flags=0):
[183] Fix | Delete
"""Return the string obtained by replacing the leftmost
[184] Fix | Delete
non-overlapping occurrences of the pattern in string by the
[185] Fix | Delete
replacement repl. repl can be either a string or a callable;
[186] Fix | Delete
if a string, backslash escapes in it are processed. If it is
[187] Fix | Delete
a callable, it's passed the match object and must return
[188] Fix | Delete
a replacement string to be used."""
[189] Fix | Delete
return _compile(pattern, flags).sub(repl, string, count)
[190] Fix | Delete
[191] Fix | Delete
def subn(pattern, repl, string, count=0, flags=0):
[192] Fix | Delete
"""Return a 2-tuple containing (new_string, number).
[193] Fix | Delete
new_string is the string obtained by replacing the leftmost
[194] Fix | Delete
non-overlapping occurrences of the pattern in the source
[195] Fix | Delete
string by the replacement repl. number is the number of
[196] Fix | Delete
substitutions that were made. repl can be either a string or a
[197] Fix | Delete
callable; if a string, backslash escapes in it are processed.
[198] Fix | Delete
If it is a callable, it's passed the match object and must
[199] Fix | Delete
return a replacement string to be used."""
[200] Fix | Delete
return _compile(pattern, flags).subn(repl, string, count)
[201] Fix | Delete
[202] Fix | Delete
def split(pattern, string, maxsplit=0, flags=0):
[203] Fix | Delete
"""Split the source string by the occurrences of the pattern,
[204] Fix | Delete
returning a list containing the resulting substrings. If
[205] Fix | Delete
capturing parentheses are used in pattern, then the text of all
[206] Fix | Delete
groups in the pattern are also returned as part of the resulting
[207] Fix | Delete
list. If maxsplit is nonzero, at most maxsplit splits occur,
[208] Fix | Delete
and the remainder of the string is returned as the final element
[209] Fix | Delete
of the list."""
[210] Fix | Delete
return _compile(pattern, flags).split(string, maxsplit)
[211] Fix | Delete
[212] Fix | Delete
def findall(pattern, string, flags=0):
[213] Fix | Delete
"""Return a list of all non-overlapping matches in the string.
[214] Fix | Delete
[215] Fix | Delete
If one or more capturing groups are present in the pattern, return
[216] Fix | Delete
a list of groups; this will be a list of tuples if the pattern
[217] Fix | Delete
has more than one group.
[218] Fix | Delete
[219] Fix | Delete
Empty matches are included in the result."""
[220] Fix | Delete
return _compile(pattern, flags).findall(string)
[221] Fix | Delete
[222] Fix | Delete
def finditer(pattern, string, flags=0):
[223] Fix | Delete
"""Return an iterator over all non-overlapping matches in the
[224] Fix | Delete
string. For each match, the iterator returns a match object.
[225] Fix | Delete
[226] Fix | Delete
Empty matches are included in the result."""
[227] Fix | Delete
return _compile(pattern, flags).finditer(string)
[228] Fix | Delete
[229] Fix | Delete
def compile(pattern, flags=0):
[230] Fix | Delete
"Compile a regular expression pattern, returning a pattern object."
[231] Fix | Delete
return _compile(pattern, flags)
[232] Fix | Delete
[233] Fix | Delete
def purge():
[234] Fix | Delete
"Clear the regular expression caches"
[235] Fix | Delete
_cache.clear()
[236] Fix | Delete
_compile_repl.cache_clear()
[237] Fix | Delete
[238] Fix | Delete
def template(pattern, flags=0):
[239] Fix | Delete
"Compile a template pattern, returning a pattern object"
[240] Fix | Delete
return _compile(pattern, flags|T)
[241] Fix | Delete
[242] Fix | Delete
_alphanum_str = frozenset(
[243] Fix | Delete
"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")
[244] Fix | Delete
_alphanum_bytes = frozenset(
[245] Fix | Delete
b"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")
[246] Fix | Delete
[247] Fix | Delete
def escape(pattern):
[248] Fix | Delete
"""
[249] Fix | Delete
Escape all the characters in pattern except ASCII letters, numbers and '_'.
[250] Fix | Delete
"""
[251] Fix | Delete
if isinstance(pattern, str):
[252] Fix | Delete
alphanum = _alphanum_str
[253] Fix | Delete
s = list(pattern)
[254] Fix | Delete
for i, c in enumerate(pattern):
[255] Fix | Delete
if c not in alphanum:
[256] Fix | Delete
if c == "\000":
[257] Fix | Delete
s[i] = "\\000"
[258] Fix | Delete
else:
[259] Fix | Delete
s[i] = "\\" + c
[260] Fix | Delete
return "".join(s)
[261] Fix | Delete
else:
[262] Fix | Delete
alphanum = _alphanum_bytes
[263] Fix | Delete
s = []
[264] Fix | Delete
esc = ord(b"\\")
[265] Fix | Delete
for c in pattern:
[266] Fix | Delete
if c in alphanum:
[267] Fix | Delete
s.append(c)
[268] Fix | Delete
else:
[269] Fix | Delete
if c == 0:
[270] Fix | Delete
s.extend(b"\\000")
[271] Fix | Delete
else:
[272] Fix | Delete
s.append(esc)
[273] Fix | Delete
s.append(c)
[274] Fix | Delete
return bytes(s)
[275] Fix | Delete
[276] Fix | Delete
# --------------------------------------------------------------------
[277] Fix | Delete
# internals
[278] Fix | Delete
[279] Fix | Delete
_cache = {}
[280] Fix | Delete
[281] Fix | Delete
_pattern_type = type(sre_compile.compile("", 0))
[282] Fix | Delete
[283] Fix | Delete
_MAXCACHE = 512
[284] Fix | Delete
def _compile(pattern, flags):
[285] Fix | Delete
# internal: compile pattern
[286] Fix | Delete
try:
[287] Fix | Delete
p, loc = _cache[type(pattern), pattern, flags]
[288] Fix | Delete
if loc is None or loc == _locale.setlocale(_locale.LC_CTYPE):
[289] Fix | Delete
return p
[290] Fix | Delete
except KeyError:
[291] Fix | Delete
pass
[292] Fix | Delete
if isinstance(pattern, _pattern_type):
[293] Fix | Delete
if flags:
[294] Fix | Delete
raise ValueError(
[295] Fix | Delete
"cannot process flags argument with a compiled pattern")
[296] Fix | Delete
return pattern
[297] Fix | Delete
if not sre_compile.isstring(pattern):
[298] Fix | Delete
raise TypeError("first argument must be string or compiled pattern")
[299] Fix | Delete
p = sre_compile.compile(pattern, flags)
[300] Fix | Delete
if not (flags & DEBUG):
[301] Fix | Delete
if len(_cache) >= _MAXCACHE:
[302] Fix | Delete
_cache.clear()
[303] Fix | Delete
if p.flags & LOCALE:
[304] Fix | Delete
if not _locale:
[305] Fix | Delete
return p
[306] Fix | Delete
loc = _locale.setlocale(_locale.LC_CTYPE)
[307] Fix | Delete
else:
[308] Fix | Delete
loc = None
[309] Fix | Delete
_cache[type(pattern), pattern, flags] = p, loc
[310] Fix | Delete
return p
[311] Fix | Delete
[312] Fix | Delete
@functools.lru_cache(_MAXCACHE)
[313] Fix | Delete
def _compile_repl(repl, pattern):
[314] Fix | Delete
# internal: compile replacement pattern
[315] Fix | Delete
return sre_parse.parse_template(repl, pattern)
[316] Fix | Delete
[317] Fix | Delete
def _expand(pattern, match, template):
[318] Fix | Delete
# internal: match.expand implementation hook
[319] Fix | Delete
template = sre_parse.parse_template(template, pattern)
[320] Fix | Delete
return sre_parse.expand_template(template, match)
[321] Fix | Delete
[322] Fix | Delete
def _subx(pattern, template):
[323] Fix | Delete
# internal: pattern.sub/subn implementation helper
[324] Fix | Delete
template = _compile_repl(template, pattern)
[325] Fix | Delete
if not template[0] and len(template[1]) == 1:
[326] Fix | Delete
# literal replacement
[327] Fix | Delete
return template[1][0]
[328] Fix | Delete
def filter(match, template=template):
[329] Fix | Delete
return sre_parse.expand_template(template, match)
[330] Fix | Delete
return filter
[331] Fix | Delete
[332] Fix | Delete
# register myself for pickling
[333] Fix | Delete
[334] Fix | Delete
import copyreg
[335] Fix | Delete
[336] Fix | Delete
def _pickle(p):
[337] Fix | Delete
return _compile, (p.pattern, p.flags)
[338] Fix | Delete
[339] Fix | Delete
copyreg.pickle(_pattern_type, _pickle, _compile)
[340] Fix | Delete
[341] Fix | Delete
# --------------------------------------------------------------------
[342] Fix | Delete
# experimental stuff (see python-dev discussions for details)
[343] Fix | Delete
[344] Fix | Delete
class Scanner:
[345] Fix | Delete
def __init__(self, lexicon, flags=0):
[346] Fix | Delete
from sre_constants import BRANCH, SUBPATTERN
[347] Fix | Delete
self.lexicon = lexicon
[348] Fix | Delete
# combine phrases into a compound pattern
[349] Fix | Delete
p = []
[350] Fix | Delete
s = sre_parse.Pattern()
[351] Fix | Delete
s.flags = flags
[352] Fix | Delete
for phrase, action in lexicon:
[353] Fix | Delete
gid = s.opengroup()
[354] Fix | Delete
p.append(sre_parse.SubPattern(s, [
[355] Fix | Delete
(SUBPATTERN, (gid, 0, 0, sre_parse.parse(phrase, flags))),
[356] Fix | Delete
]))
[357] Fix | Delete
s.closegroup(gid, p[-1])
[358] Fix | Delete
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
[359] Fix | Delete
self.scanner = sre_compile.compile(p)
[360] Fix | Delete
def scan(self, string):
[361] Fix | Delete
result = []
[362] Fix | Delete
append = result.append
[363] Fix | Delete
match = self.scanner.scanner(string).match
[364] Fix | Delete
i = 0
[365] Fix | Delete
while True:
[366] Fix | Delete
m = match()
[367] Fix | Delete
if not m:
[368] Fix | Delete
break
[369] Fix | Delete
j = m.end()
[370] Fix | Delete
if i == j:
[371] Fix | Delete
break
[372] Fix | Delete
action = self.lexicon[m.lastindex-1][1]
[373] Fix | Delete
if callable(action):
[374] Fix | Delete
self.match = m
[375] Fix | Delete
action = action(self, m.group())
[376] Fix | Delete
if action is not None:
[377] Fix | Delete
append(action)
[378] Fix | Delete
i = j
[379] Fix | Delete
return result, string[i:]
[380] Fix | Delete
[381] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function