当前位置: 首页>>代码示例>>Python>>正文


Python sre_parse.parse方法代码示例

本文整理汇总了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
        ) 
开发者ID:awemulya,项目名称:kobo-predict,代码行数:26,代码来源:sre_compile.py

示例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) 
开发者ID:kdart,项目名称:pycopia,代码行数:23,代码来源:framework.py

示例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)

###} 
开发者ID:lark-parser,项目名称:lark,代码行数:18,代码来源:utils.py

示例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) 
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:19,代码来源:re.py

示例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) 
开发者ID:google,项目名称:sre_yield,代码行数:23,代码来源:test_sre_yield.py

示例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 
开发者ID:pschanely,项目名称:CrossHair,代码行数:20,代码来源:relib.py

示例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") 
开发者ID:Rahix,项目名称:tbot,代码行数:18,代码来源:channel.py

示例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
    ]) 
开发者ID:duo-labs,项目名称:dlint,代码行数:18,代码来源:detect.py

示例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
        ) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:35,代码来源:sre_compile.py

示例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) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:16,代码来源:re.py

示例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) 
开发者ID:rafasashi,项目名称:razzy-spinner,代码行数:17,代码来源:regexp.py

示例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
        ) 
开发者ID:glmcdona,项目名称:meddle,代码行数:32,代码来源:sre_compile.py

示例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) 
开发者ID:awemulya,项目名称:kobo-predict,代码行数:17,代码来源:re.py

示例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) 
开发者ID:kdart,项目名称:pycopia,代码行数:16,代码来源:framework.py


注:本文中的sre_parse.parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。