[英]Issue with python version 2.7.3 importing maxrepeat module
我正在嘗試在python 2.7.3中導入maxrepeat模塊,但在Google中無法獲取太多信息,請有人幫忙。
什么是可幫助maxrepeat模塊工作的模塊?
我能夠使用“ from _sre import maxrepeat”來導入maxrepeat模塊,但是在運行runnninv Automation時仍然失敗。
re
模塊內部將MAXREPEAT
用作可在模式中指定的最小,最大或精確重復次數的上限。 例如:
>>> 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
重復值MAXREPEAT
或超過MAXREPEAT
會導致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
正常使用re
模塊時,沒有任何理由要關心MAXREPEAT
。 如果需要處理錯誤,請使用異常:
try:
re.compile(r'a{{{}}}'.format(MAXREPEAT))
except OverflowError as exc:
print 'Failed to compile pattern: {}'.format(exc.message)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.