>> re.search(r"a{1,99999}",-6ren">
gpt4 book ai didi

python - Python 正则表达式中允许的最大重复次数是多少?

转载 作者:太空狗 更新时间:2023-10-29 17:45:59 25 4
gpt4 key购买 nike

在 Python 2.7 和 3 中,以下工作:

>>> re.search(r"a{1,9999}", 'aaa')
<_sre.SRE_Match object at 0x1f5d100>

但这给出了一个错误:

>>> re.search(r"a{1,99999}", 'aaa')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/re.py", line 142, in search
return _compile(pattern, flags).search(string)
File "/usr/lib/python2.7/re.py", line 240, in _compile
p = sre_compile.compile(pattern, flags)
File "/usr/lib/python2.7/sre_compile.py", line 523, in compile
groupindex, indexgroup
RuntimeError: invalid SRE code

好像有重复次数的上限。这是正则表达式规范的一部分,还是特定于 Python 的限制?如果特定于 Python,是否在某处记录了实际数字,它是否因实现而异?

最佳答案

快速手动二进制搜索揭示了答案,特别是 65535:

>>> re.search(r"a{1,65535}", 'aaa')
<_sre.SRE_Match object at 0x2a9a68>
>>>
>>> re.search(r"a{1,65536}", 'aaa')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/re.py", line 142, in search
return _compile(pattern, flags).search(string)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/re.py", line 240, in _compile
p = sre_compile.compile(pattern, flags)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/sre_compile.py", line 523, in compile
groupindex, indexgroup
OverflowError: regular expression code size limit exceeded

这是讨论 here :

The limit is an implementation detail. The pattern is compiled into codes which are then interpreted, and it just happens that the codes are (usually) 16 bits, giving a range of 0..65535, but it uses 65535 to represent no limit and doesn't warn if you actually write 65535.

The quantifiers use 65535 to represent no upper limit, so ".{0,65535}" is equivalent to ".*".


感谢以下评论的作者指出了更多内容:

  • CPython 在 _sre.c 中实现了这个限制. (@LukasGraf)
  • sre_constants.py 中有一个常量MAXREPEAT拥有这个最大重复值:

    >>> import sre_constants
    >>>
    >>> sre_constants.MAXREPEAT
    65535

    (@MarkkuK。和@hcwhsa)

关于python - Python 正则表达式中允许的最大重复次数是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19773621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com