代码之家  ›  专栏  ›  技术社区  ›  sivakumar

python 2.7.3版导入maxrepeat模块时出现问题

  •  0
  • sivakumar  · 技术社区  · 7 年前

    我可以使用from sre import MAXREAT导入MAXREAT模块,但在运行nNINV automation时仍然失败。

    1 回复  |  直到 7 年前
        1
  •  2
  •   mhawke    7 年前

    MAXREPEAT 已使用 re 模块作为可在模式中指定的最小、最大或精确重复次数的上限。例如:

    >>> import re
    >>> re.compile(r'a{100}')         # exactly 100 "a"s
    <_sre.SRE_Pattern object at 0x7fa68be10780>
    >>> re.compile(r'a{100, 200}')    # between 100 and 200 "a"s
    

    等于或超过 最大重复 在重复中,值导致模块中的正则表达式解析器引发异常 sre_parse

    >>> from sre_constants import MAXREPEAT
    >>> MAXREPEAT
    4294967295L
    
    >>> re.compile(r'a{{{}}}'.format(MAXREPEAT-1))
    <_sre.SRE_Pattern object at 0x7f0ec959f660>
    
    >>> re.compile(r'a{{{}}}'.format(MAXREPEAT))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib64/python2.7/re.py", line 194, in compile
        return _compile(pattern, flags)
      File "/usr/lib64/python2.7/re.py", line 249, in _compile
        p = sre_compile.compile(pattern, flags)
      File "/usr/lib64/python2.7/sre_compile.py", line 572, in compile
        p = sre_parse.parse(p, flags)
      File "/usr/lib64/python2.7/sre_parse.py", line 716, in parse
        p = _parse_sub(source, pattern, 0)
      File "/usr/lib64/python2.7/sre_parse.py", line 324, in _parse_sub
        itemsappend(_parse(source, state))
      File "/usr/lib64/python2.7/sre_parse.py", line 518, in _parse
        raise OverflowError("the repetition number is too large")
    OverflowError: the repetition number is too large
    

    不应该有任何理由关心 重新

    try:
        re.compile(r'a{{{}}}'.format(MAXREPEAT))
    except OverflowError as exc:
        print 'Failed to compile pattern: {}'.format(exc.message)