Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
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
(?iLmsux) Set the 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].
[66] Fix | Delete
\D Matches any non-digit character; equivalent to the set [^0-9].
[67] Fix | Delete
\s Matches any whitespace character; equivalent to [ \t\n\r\f\v].
[68] Fix | Delete
\S Matches any non-whitespace character; equiv. to [^ \t\n\r\f\v].
[69] Fix | Delete
\w Matches any alphanumeric character; equivalent to [a-zA-Z0-9_].
[70] Fix | Delete
With LOCALE, it will match the set [0-9_] plus characters defined
[71] Fix | Delete
as letters for the current locale.
[72] Fix | Delete
\W Matches the complement of \w.
[73] Fix | Delete
\\ Matches a literal backslash.
[74] Fix | Delete
[75] Fix | Delete
This module exports the following functions:
[76] Fix | Delete
match Match a regular expression pattern to the beginning of a string.
[77] Fix | Delete
search Search a string for the presence of a pattern.
[78] Fix | Delete
sub Substitute occurrences of a pattern found in a string.
[79] Fix | Delete
subn Same as sub, but also return the number of substitutions made.
[80] Fix | Delete
split Split a string by the occurrences of a pattern.
[81] Fix | Delete
findall Find all occurrences of a pattern in a string.
[82] Fix | Delete
finditer Return an iterator yielding a match object for each match.
[83] Fix | Delete
compile Compile a pattern into a RegexObject.
[84] Fix | Delete
purge Clear the regular expression cache.
[85] Fix | Delete
escape Backslash all non-alphanumerics in a string.
[86] Fix | Delete
[87] Fix | Delete
Some of the functions in this module takes flags as optional parameters:
[88] Fix | Delete
I IGNORECASE Perform case-insensitive matching.
[89] Fix | Delete
L LOCALE Make \w, \W, \b, \B, dependent on the current locale.
[90] Fix | Delete
M MULTILINE "^" matches the beginning of lines (after a newline)
[91] Fix | Delete
as well as the string.
[92] Fix | Delete
"$" matches the end of lines (before a newline) as well
[93] Fix | Delete
as the end of the string.
[94] Fix | Delete
S DOTALL "." matches any character at all, including the newline.
[95] Fix | Delete
X VERBOSE Ignore whitespace and comments for nicer looking RE's.
[96] Fix | Delete
U UNICODE Make \w, \W, \b, \B, dependent on the Unicode locale.
[97] Fix | Delete
[98] Fix | Delete
This module also defines an exception 'error'.
[99] Fix | Delete
[100] Fix | Delete
"""
[101] Fix | Delete
[102] Fix | Delete
import sys
[103] Fix | Delete
import sre_compile
[104] Fix | Delete
import sre_parse
[105] Fix | Delete
try:
[106] Fix | Delete
import _locale
[107] Fix | Delete
except ImportError:
[108] Fix | Delete
_locale = None
[109] Fix | Delete
[110] Fix | Delete
# public symbols
[111] Fix | Delete
__all__ = [ "match", "search", "sub", "subn", "split", "findall",
[112] Fix | Delete
"compile", "purge", "template", "escape", "I", "L", "M", "S", "X",
[113] Fix | Delete
"U", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
[114] Fix | Delete
"UNICODE", "error" ]
[115] Fix | Delete
[116] Fix | Delete
__version__ = "2.2.1"
[117] Fix | Delete
[118] Fix | Delete
# flags
[119] Fix | Delete
I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case
[120] Fix | Delete
L = LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
[121] Fix | Delete
U = UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode locale
[122] Fix | Delete
M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline
[123] Fix | Delete
S = DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline
[124] Fix | Delete
X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments
[125] Fix | Delete
[126] Fix | Delete
# sre extensions (experimental, don't rely on these)
[127] Fix | Delete
T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
[128] Fix | Delete
DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
[129] Fix | Delete
[130] Fix | Delete
# sre exception
[131] Fix | Delete
error = sre_compile.error
[132] Fix | Delete
[133] Fix | Delete
# --------------------------------------------------------------------
[134] Fix | Delete
# public interface
[135] Fix | Delete
[136] Fix | Delete
def match(pattern, string, flags=0):
[137] Fix | Delete
"""Try to apply the pattern at the start of the string, returning
[138] Fix | Delete
a match object, or None if no match was found."""
[139] Fix | Delete
return _compile(pattern, flags).match(string)
[140] Fix | Delete
[141] Fix | Delete
def search(pattern, string, flags=0):
[142] Fix | Delete
"""Scan through string looking for a match to the pattern, returning
[143] Fix | Delete
a match object, or None if no match was found."""
[144] Fix | Delete
return _compile(pattern, flags).search(string)
[145] Fix | Delete
[146] Fix | Delete
def sub(pattern, repl, string, count=0, flags=0):
[147] Fix | Delete
"""Return the string obtained by replacing the leftmost
[148] Fix | Delete
non-overlapping occurrences of the pattern in string by the
[149] Fix | Delete
replacement repl. repl can be either a string or a callable;
[150] Fix | Delete
if a string, backslash escapes in it are processed. If it is
[151] Fix | Delete
a callable, it's passed the match object and must return
[152] Fix | Delete
a replacement string to be used."""
[153] Fix | Delete
return _compile(pattern, flags).sub(repl, string, count)
[154] Fix | Delete
[155] Fix | Delete
def subn(pattern, repl, string, count=0, flags=0):
[156] Fix | Delete
"""Return a 2-tuple containing (new_string, number).
[157] Fix | Delete
new_string is the string obtained by replacing the leftmost
[158] Fix | Delete
non-overlapping occurrences of the pattern in the source
[159] Fix | Delete
string by the replacement repl. number is the number of
[160] Fix | Delete
substitutions that were made. repl can be either a string or a
[161] Fix | Delete
callable; if a string, backslash escapes in it are processed.
[162] Fix | Delete
If it is a callable, it's passed the match object and must
[163] Fix | Delete
return a replacement string to be used."""
[164] Fix | Delete
return _compile(pattern, flags).subn(repl, string, count)
[165] Fix | Delete
[166] Fix | Delete
def split(pattern, string, maxsplit=0, flags=0):
[167] Fix | Delete
"""Split the source string by the occurrences of the pattern,
[168] Fix | Delete
returning a list containing the resulting substrings."""
[169] Fix | Delete
return _compile(pattern, flags).split(string, maxsplit)
[170] Fix | Delete
[171] Fix | Delete
def findall(pattern, string, flags=0):
[172] Fix | Delete
"""Return a list of all non-overlapping matches in the string.
[173] Fix | Delete
[174] Fix | Delete
If one or more groups are present in the pattern, return a
[175] Fix | Delete
list of groups; this will be a list of tuples if the pattern
[176] Fix | Delete
has more than one group.
[177] Fix | Delete
[178] Fix | Delete
Empty matches are included in the result."""
[179] Fix | Delete
return _compile(pattern, flags).findall(string)
[180] Fix | Delete
[181] Fix | Delete
if sys.hexversion >= 0x02020000:
[182] Fix | Delete
__all__.append("finditer")
[183] Fix | Delete
def finditer(pattern, string, flags=0):
[184] Fix | Delete
"""Return an iterator over all non-overlapping matches in the
[185] Fix | Delete
string. For each match, the iterator returns a match object.
[186] Fix | Delete
[187] Fix | Delete
Empty matches are included in the result."""
[188] Fix | Delete
return _compile(pattern, flags).finditer(string)
[189] Fix | Delete
[190] Fix | Delete
def compile(pattern, flags=0):
[191] Fix | Delete
"Compile a regular expression pattern, returning a pattern object."
[192] Fix | Delete
return _compile(pattern, flags)
[193] Fix | Delete
[194] Fix | Delete
def purge():
[195] Fix | Delete
"Clear the regular expression cache"
[196] Fix | Delete
_cache.clear()
[197] Fix | Delete
_cache_repl.clear()
[198] Fix | Delete
[199] Fix | Delete
def template(pattern, flags=0):
[200] Fix | Delete
"Compile a template pattern, returning a pattern object"
[201] Fix | Delete
return _compile(pattern, flags|T)
[202] Fix | Delete
[203] Fix | Delete
_alphanum = frozenset(
[204] Fix | Delete
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
[205] Fix | Delete
[206] Fix | Delete
def escape(pattern):
[207] Fix | Delete
"Escape all non-alphanumeric characters in pattern."
[208] Fix | Delete
s = list(pattern)
[209] Fix | Delete
alphanum = _alphanum
[210] Fix | Delete
for i, c in enumerate(pattern):
[211] Fix | Delete
if c not in alphanum:
[212] Fix | Delete
if c == "\000":
[213] Fix | Delete
s[i] = "\\000"
[214] Fix | Delete
else:
[215] Fix | Delete
s[i] = "\\" + c
[216] Fix | Delete
return pattern[:0].join(s)
[217] Fix | Delete
[218] Fix | Delete
# --------------------------------------------------------------------
[219] Fix | Delete
# internals
[220] Fix | Delete
[221] Fix | Delete
_cache = {}
[222] Fix | Delete
_cache_repl = {}
[223] Fix | Delete
[224] Fix | Delete
_pattern_type = type(sre_compile.compile("", 0))
[225] Fix | Delete
[226] Fix | Delete
_MAXCACHE = 100
[227] Fix | Delete
[228] Fix | Delete
def _compile(*key):
[229] Fix | Delete
# internal: compile pattern
[230] Fix | Delete
pattern, flags = key
[231] Fix | Delete
bypass_cache = flags & DEBUG
[232] Fix | Delete
if not bypass_cache:
[233] Fix | Delete
cachekey = (type(key[0]),) + key
[234] Fix | Delete
try:
[235] Fix | Delete
p, loc = _cache[cachekey]
[236] Fix | Delete
if loc is None or loc == _locale.setlocale(_locale.LC_CTYPE):
[237] Fix | Delete
return p
[238] Fix | Delete
except KeyError:
[239] Fix | Delete
pass
[240] Fix | Delete
if isinstance(pattern, _pattern_type):
[241] Fix | Delete
if flags:
[242] Fix | Delete
raise ValueError('Cannot process flags argument with a compiled pattern')
[243] Fix | Delete
return pattern
[244] Fix | Delete
if not sre_compile.isstring(pattern):
[245] Fix | Delete
raise TypeError, "first argument must be string or compiled pattern"
[246] Fix | Delete
try:
[247] Fix | Delete
p = sre_compile.compile(pattern, flags)
[248] Fix | Delete
except error, v:
[249] Fix | Delete
raise error, v # invalid expression
[250] Fix | Delete
if not bypass_cache:
[251] Fix | Delete
if len(_cache) >= _MAXCACHE:
[252] Fix | Delete
_cache.clear()
[253] Fix | Delete
if p.flags & LOCALE:
[254] Fix | Delete
if not _locale:
[255] Fix | Delete
return p
[256] Fix | Delete
loc = _locale.setlocale(_locale.LC_CTYPE)
[257] Fix | Delete
else:
[258] Fix | Delete
loc = None
[259] Fix | Delete
_cache[cachekey] = p, loc
[260] Fix | Delete
return p
[261] Fix | Delete
[262] Fix | Delete
def _compile_repl(*key):
[263] Fix | Delete
# internal: compile replacement pattern
[264] Fix | Delete
p = _cache_repl.get(key)
[265] Fix | Delete
if p is not None:
[266] Fix | Delete
return p
[267] Fix | Delete
repl, pattern = key
[268] Fix | Delete
try:
[269] Fix | Delete
p = sre_parse.parse_template(repl, pattern)
[270] Fix | Delete
except error, v:
[271] Fix | Delete
raise error, v # invalid expression
[272] Fix | Delete
if len(_cache_repl) >= _MAXCACHE:
[273] Fix | Delete
_cache_repl.clear()
[274] Fix | Delete
_cache_repl[key] = p
[275] Fix | Delete
return p
[276] Fix | Delete
[277] Fix | Delete
def _expand(pattern, match, template):
[278] Fix | Delete
# internal: match.expand implementation hook
[279] Fix | Delete
template = sre_parse.parse_template(template, pattern)
[280] Fix | Delete
return sre_parse.expand_template(template, match)
[281] Fix | Delete
[282] Fix | Delete
def _subx(pattern, template):
[283] Fix | Delete
# internal: pattern.sub/subn implementation helper
[284] Fix | Delete
template = _compile_repl(template, pattern)
[285] Fix | Delete
if not template[0] and len(template[1]) == 1:
[286] Fix | Delete
# literal replacement
[287] Fix | Delete
return template[1][0]
[288] Fix | Delete
def filter(match, template=template):
[289] Fix | Delete
return sre_parse.expand_template(template, match)
[290] Fix | Delete
return filter
[291] Fix | Delete
[292] Fix | Delete
# register myself for pickling
[293] Fix | Delete
[294] Fix | Delete
import copy_reg
[295] Fix | Delete
[296] Fix | Delete
def _pickle(p):
[297] Fix | Delete
return _compile, (p.pattern, p.flags)
[298] Fix | Delete
[299] Fix | Delete
copy_reg.pickle(_pattern_type, _pickle, _compile)
[300] Fix | Delete
[301] Fix | Delete
# --------------------------------------------------------------------
[302] Fix | Delete
# experimental stuff (see python-dev discussions for details)
[303] Fix | Delete
[304] Fix | Delete
class Scanner:
[305] Fix | Delete
def __init__(self, lexicon, flags=0):
[306] Fix | Delete
from sre_constants import BRANCH, SUBPATTERN
[307] Fix | Delete
self.lexicon = lexicon
[308] Fix | Delete
# combine phrases into a compound pattern
[309] Fix | Delete
p = []
[310] Fix | Delete
s = sre_parse.Pattern()
[311] Fix | Delete
s.flags = flags
[312] Fix | Delete
for phrase, action in lexicon:
[313] Fix | Delete
p.append(sre_parse.SubPattern(s, [
[314] Fix | Delete
(SUBPATTERN, (len(p)+1, sre_parse.parse(phrase, flags))),
[315] Fix | Delete
]))
[316] Fix | Delete
s.groups = len(p)+1
[317] Fix | Delete
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
[318] Fix | Delete
self.scanner = sre_compile.compile(p)
[319] Fix | Delete
def scan(self, string):
[320] Fix | Delete
result = []
[321] Fix | Delete
append = result.append
[322] Fix | Delete
match = self.scanner.scanner(string).match
[323] Fix | Delete
i = 0
[324] Fix | Delete
while 1:
[325] Fix | Delete
m = match()
[326] Fix | Delete
if not m:
[327] Fix | Delete
break
[328] Fix | Delete
j = m.end()
[329] Fix | Delete
if i == j:
[330] Fix | Delete
break
[331] Fix | Delete
action = self.lexicon[m.lastindex-1][1]
[332] Fix | Delete
if hasattr(action, '__call__'):
[333] Fix | Delete
self.match = m
[334] Fix | Delete
action = action(self, m.group())
[335] Fix | Delete
if action is not None:
[336] Fix | Delete
append(action)
[337] Fix | Delete
i = j
[338] Fix | Delete
return result, string[i:]
[339] Fix | Delete
[340] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function