我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用sre_parse.parse()。
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 )
def regex(regex): """Return strategy that generates strings that match given regex. Regex can be either a string or compiled regex (through `re.compile()`). You can use regex flags (such as `re.IGNORECASE`, `re.DOTALL` or `re.UNICODE`) to control generation. Flags can be passed either in compiled regex (specify flags in call to `re.compile()`) or inside pattern with (?iLmsux) group. Some tricky regular expressions are partly supported or not supported at all. "^" and "$" do not affect generation. Positive lookahead/lookbehind groups are considered normal groups. Negative lookahead/lookbehind groups do not do anything. Ternary regex groups ('(?(name)yes-pattern|no-pattern)') are not supported at all. """ if not hasattr(regex, 'pattern'): regex = re.compile(regex) pattern = regex.pattern flags = regex.flags codes = sre.parse(pattern) return _strategy(codes, Context(flags=flags)).filter(regex.match)
def __call__(self, input, *args, **kws): """ parse a string starting from the default axiom The default axiom is START. Parameters: input : input string to parse *args : argument list to pass to START **kws : argument dictionnary to pass to START If a line ends with '::', it is considered as the end of the ReST part. The lines after are the grammar. """ docs = re.split(r"::[ \t]*$", input, maxsplit=1, flags=re.M) if len(docs) == 2: input = re.sub(".", " ", docs[0])+docs[1] return self.parse('START', input, *args, **kws)
def __init__(self, rules, flags=0): pattern = Pattern() pattern.flags = flags if sys.version_info < (3, 0): pattern.groups = len(rules) + 1 _og = pattern.opengroup pattern.opengroup = lambda n: _og(n and '%s\x00%s' % (name, n) or n) self.rules = [] subpatterns = [] for group, (name, regex) in enumerate(rules, 1): last_group = pattern.groups - 1 subpatterns.append( SubPattern(pattern, [ (SUBPATTERN, (group, parse(regex, flags, pattern))), ])) self.rules.append((name, last_group, pattern.groups - 1)) self._scanner = sre_compile( SubPattern(pattern, [(BRANCH, (None, subpatterns))])).scanner
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 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 )
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)