我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用sre_parse.SubPattern()。
def visit(self, node): if not isinstance(node, sre_parse.SubPattern): raise TypeError('{0!r} is not a subpattern'.format(node)) for op, args in node.data: if not isinstance(args, (list, tuple)): args = (args,) op = normalize_token(op) method = 'visit_' + op visitor = getattr(self, method, None) original_flags = self.flags try: if self.flags is None: self.flags = node.pattern.flags if self.flags & re.IGNORECASE: for flag in locale_flags.values(): self.justified_flags |= flag if visitor is not None: ts = visitor(*args) or () for t in ts: yield t for t in self.generic_visit(*args): yield t finally: self.flags = original_flags
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, 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)
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)
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)
def generic_visit(self, *args): for arg in args: if isinstance(arg, (list, tuple)): for t in self.generic_visit(*arg): yield t elif isinstance(arg, sre_parse.SubPattern): for t in self.visit(arg): yield t elif isinstance(arg, (int, long, str)): pass elif arg is None: pass else: raise TypeError('{0!r} has unexpected type'.format(arg))