本文整理匯總了Python中sre_parse.parse方法的典型用法代碼示例。如果您正苦於以下問題:Python sre_parse.parse方法的具體用法?Python sre_parse.parse怎麽用?Python sre_parse.parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sre_parse
的用法示例。
在下文中一共展示了sre_parse.parse方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: compile
# 需要導入模塊: import sre_parse [as 別名]
# 或者: from sre_parse import parse [as 別名]
def compile(p, flags=0):
# internal: convert pattern list to internal format
if isstring(p):
pattern = p
p = sre_parse.parse(p, flags)
else:
pattern = None
code = _code(p, flags)
# print(code)
# map in either direction
groupindex = p.pattern.groupdict
indexgroup = [None] * p.pattern.groups
for k, i in groupindex.items():
indexgroup[i] = k
return _sre.compile(
pattern, flags | p.pattern.flags, code,
p.pattern.groups-1,
groupindex, indexgroup
)
示例2: __repr__
# 需要導入模塊: import sre_parse [as 別名]
# 或者: from sre_parse import parse [as 別名]
def __repr__(self):
# Since this is called as part of error handling, we need to be very
# robust against potentially malformed input.
try:
get = pformat(self.GET)
except:
get = '<could not parse>'
try:
post = pformat(self.POST)
except:
post = '<could not parse>'
try:
cookies = pformat(self.COOKIES)
except:
cookies = '<could not parse>'
try:
meta = pformat(self.environ)
except:
meta = '<could not parse>'
return '<HTTPRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nenviron:%s>' % \
(get, post, cookies, meta)
示例3: get_regexp_width
# 需要導入模塊: import sre_parse [as 別名]
# 或者: from sre_parse import parse [as 別名]
def get_regexp_width(expr):
if regex:
# Since `sre_parse` cannot deal with Unicode categories of the form `\p{Mn}`, we replace these with
# a simple letter, which makes no difference as we are only trying to get the possible lengths of the regex
# match here below.
regexp_final = re.sub(categ_pattern, 'A', expr)
else:
if re.search(categ_pattern, expr):
raise ImportError('`regex` module must be installed in order to use Unicode categories.', expr)
regexp_final = expr
try:
return [int(x) for x in sre_parse.parse(regexp_final).getwidth()]
except sre_constants.error:
raise ValueError(expr)
###}
示例4: __init__
# 需要導入模塊: import sre_parse [as 別名]
# 或者: from sre_parse import parse [as 別名]
def __init__(self, lexicon, flags=0):
from sre_constants import BRANCH, SUBPATTERN
if isinstance(flags, RegexFlag):
flags = flags.value
self.lexicon = lexicon
# combine phrases into a compound pattern
p = []
s = sre_parse.Pattern()
s.flags = flags
for phrase, action in lexicon:
gid = s.opengroup()
p.append(sre_parse.SubPattern(s, [
(SUBPATTERN, (gid, 0, 0, sre_parse.parse(phrase, flags))),
]))
s.closegroup(gid, p[-1])
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
self.scanner = sre_compile.compile(p)
示例5: testPreparsedInstantiation
# 需要導入模塊: import sre_parse [as 別名]
# 或者: from sre_parse import parse [as 別名]
def testPreparsedInstantiation(self):
self.assertSequenceEqual(sre_yield.AllStrings(r"(?:[aeiou])\Z"), list("aeiou"))
preparsed = sre_parse.parse("[aeiou]")
self.assertSequenceEqual(sre_yield.AllStrings(preparsed), list("aeiou"))
preparsed = sre_parse.parse(r"(?:[aeiou])\Z")
self.assertSequenceEqual(sre_yield.AllStrings(preparsed), list("aeiou"))
preparsed = sre_parse.parse("[01]+")
parsed = sre_yield.AllStrings(preparsed)
self.assertTrue("0101" in parsed)
self.assertFalse("0201" in parsed)
preparsed = sre_parse.parse("[01]+")
parsed = sre_yield.AllStrings(preparsed)
self.assertTrue("0101" in parsed)
self.assertFalse("0201" in parsed)
preparsed = sre_parse.parse(r"(?:[01]+)\Z")
parsed = sre_yield.AllStrings(preparsed)
self.assertTrue("0101" in parsed)
self.assertFalse("0201" in parsed)
示例6: _match_pattern
# 需要導入模塊: import sre_parse [as 別名]
# 或者: from sre_parse import parse [as 別名]
def _match_pattern(compiled_regex, pattern, orig_smtstr, pos, endpos=None):
space = orig_smtstr.statespace
parsed_pattern = parse(pattern, compiled_regex.flags)
smtstr = _slice_match_area(orig_smtstr, pos, endpos)
match = _internal_match_patterns(space, parsed_pattern, compiled_regex.flags, smtstr, pos)
if match is not None:
match.pos = pos
match.endpos = endpos if endpos is not None else len(orig_smtstr)
match.re = compiled_regex
match.string = orig_smtstr
# fill None in unmatched groups:
while len(match._groups) < compiled_regex.groups + 1:
match._groups.append(None)
# Link up any named groups:
for name, num in compiled_regex.groupindex.items():
(_, start, end) = match._groups[num]
match._groups[num] = (name, start, end)
return match
示例7: __init__
# 需要導入模塊: import sre_parse [as 別名]
# 或者: from sre_parse import parse [as 別名]
def __init__(self, pattern: typing.Pattern[bytes]) -> None:
self.pattern = pattern
import sre_parse
parsed = sre_parse.parse(
typing.cast(str, self.pattern.pattern), flags=self.pattern.flags
)
width = parsed.getwidth()
if isinstance(width, int):
self._length = width
elif isinstance(width, tuple):
self._length = width[1]
if self._length == getattr(sre_parse, "MAXREPEAT", 2 ** 32 - 1):
raise Exception(f"Expression {self.pattern.pattern!r} is not bounded")
示例8: catastrophic
# 需要導入模塊: import sre_parse [as 別名]
# 或者: from sre_parse import parse [as 別名]
def catastrophic(pattern):
try:
subpattern = sre_parse.parse(pattern)
except sre_constants.error:
return False
root = OpNode(None, ())
build_op_tree(root, subpattern)
nested_quantifiers = max_nested_quantifiers(root) > 1
alternation = mutually_inclusive_alternation(root)
return any([
nested_quantifiers,
alternation
])
示例9: compile
# 需要導入模塊: import sre_parse [as 別名]
# 或者: from sre_parse import parse [as 別名]
def compile(p, flags=0):
# internal: convert pattern list to internal format
#print("sre_compile.py:compile:504:p", p)
if isstring(p):
pattern = p
p = sre_parse.parse(p, flags)
else:
pattern = None
#print('sre_compile.py:498:p', p)
code = _code(p, flags)
#print('sre_compile.py:501:code', code)
# print code
# XXX: <fl> get rid of this limitation!
if p.pattern.groups > 100:
raise AssertionError(
"sorry, but this version only supports 100 named groups"
)
# map in either direction
groupindex = p.pattern.groupdict
indexgroup = [None] * p.pattern.groups
for k, i in groupindex.items():
indexgroup[i] = k
return _sre.compile(
pattern, flags | p.pattern.flags, code,
p.pattern.groups-1,
groupindex, indexgroup
)
示例10: __init__
# 需要導入模塊: import sre_parse [as 別名]
# 或者: from sre_parse import parse [as 別名]
def __init__(self, lexicon, flags=0):
from sre_constants import BRANCH, SUBPATTERN
self.lexicon = lexicon
# combine phrases into a compound pattern
p = []
s = sre_parse.Pattern()
s.flags = flags
for phrase, action in lexicon:
p.append(sre_parse.SubPattern(s, [
(SUBPATTERN, (len(p)+1, sre_parse.parse(phrase, flags))),
]))
s.groups = len(p)+1
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
self.scanner = sre_compile.compile(p)
示例11: _compile
# 需要導入模塊: import sre_parse [as 別名]
# 或者: from sre_parse import parse [as 別名]
def _compile(regexp):
parsed = sre_parse.parse(regexp)
parsed = _remove_group_identifiers(parsed)
# Add grouping parentheses around the regexp; this will allow
# us to access the material that was split on.
# Need to set the Pattern to expect a single group
pattern = sre_parse.Pattern()
pattern.groups += 1
grouped = sre_parse.SubPattern(pattern)
grouped.append((sre_constants.SUBPATTERN, (1, parsed)))
return sre_compile.compile(grouped, re.UNICODE | re.MULTILINE | re.DOTALL)
示例12: compile
# 需要導入模塊: import sre_parse [as 別名]
# 或者: from sre_parse import parse [as 別名]
def compile(p, flags=0):
# internal: convert pattern list to internal format
if isstring(p):
pattern = p
p = sre_parse.parse(p, flags)
else:
pattern = None
code = _code(p, flags)
# print code
# XXX: <fl> get rid of this limitation!
if p.pattern.groups > 100:
raise AssertionError(
"sorry, but this version only supports 100 named groups"
)
# map in either direction
groupindex = p.pattern.groupdict
indexgroup = [None] * p.pattern.groups
for k, i in groupindex.items():
indexgroup[i] = k
return _sre.compile(
pattern, flags | p.pattern.flags, code,
p.pattern.groups-1,
groupindex, indexgroup
)
示例13: __init__
# 需要導入模塊: import sre_parse [as 別名]
# 或者: from sre_parse import parse [as 別名]
def __init__(self, lexicon, flags=0):
from sre_constants import BRANCH, SUBPATTERN
self.lexicon = lexicon
# combine phrases into a compound pattern
p = []
s = sre_parse.Pattern()
s.flags = flags
for phrase, action in lexicon:
gid = s.opengroup()
p.append(sre_parse.SubPattern(s, [
(SUBPATTERN, (gid, sre_parse.parse(phrase, flags))),
]))
s.closegroup(gid, p[-1])
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
self.scanner = sre_compile.compile(p)
示例14: _make_url_form
# 需要導入模塊: import sre_parse [as 別名]
# 或者: from sre_parse import parse [as 別名]
def _make_url_form(regexp):
# Build reverse mapping format from RE parse tree. This simplified function
# only works with the type of RE used in url mappings in the fcgi
# config file.
cre = re.compile(regexp, re.I)
indexmap = dict([(v,k) for k,v in cre.groupindex.items()])
collect = []
for op, val in sre_parse.parse(regexp, re.I):
if op is sre_parse.LITERAL:
collect.append(chr(val))
elif op is sre_parse.SUBPATTERN:
name = indexmap[val[0]]
collect.append(br'%%(%s)s' % name)
return cre, "".join(collect)