Python 重新搜索变得没有响应

Python 重新搜索变得没有响应,python,regex,Python,Regex,当我运行此代码时,它既不打印“选中”也不打印“不匹配”。它完全停止响应 url='http://hoswifi.bblink.cn/v3/2-fd1cc0657845832e5e1248e6539a50fa/topic/55-13950.html?from=home' m=re.search(r'/\d-(B|(\w+){10,64})/index.html',url) if m: print('checked') else: print('not matching') 这是

当我运行此代码时,它既不打印
“选中”也不打印
“不匹配”
。它完全停止响应

url='http://hoswifi.bblink.cn/v3/2-fd1cc0657845832e5e1248e6539a50fa/topic/55-13950.html?from=home'

m=re.search(r'/\d-(B|(\w+){10,64})/index.html',url)
if m:
    print('checked')
else:
    print('not matching')
这是对的

“\w+{10,64}”错误,\w+不应与“+”连用

   url='http://hoswifi.bblink.cn/v3/2-fd1cc0657845832e5e1248e6539a50fa/topic/55-13950.html?from=home'
m=re.search(r'/\d-(B|\w{10,64})/index.html',url)
if m:
    print('checked')
else:
    print('not matching')

假设我们有以下脚本:

s = '1234567890'    
m = re.search(r'(\w+)*z', s)
我们的字符串包含10位数字,不包含
'z'
。这是有意的,因此它强制
re.search
检查所有可能的组合,否则它将在第一次匹配时停止

我无法计算可能的组合数,因为涉及的数学问题相当棘手,但这里有一个小演示,演示当
s
获得更多数字时会发生什么:

时间从一位数
s
的约1μs到30位数
s
的约100秒,即多出108秒


我猜当您使用
(\w+{10,64}
时,也会发生类似的情况。相反,您应该使用
\w{10,64}


用于演示的代码:

import timeit
import matplotlib.pyplot as plt

setup = """
import re
"""    
_base_stmt = "m = re.search(r'(\w+)*z','{}')"

# (searched string becomes '1', '11', '111'...)
statements = {}
for i in range(1, 18):
    statements.update({i: _base_stmt.format('1'*i)})

# Creates x, y values
x = []
y = []
for i in sorted(statements):
    x.append(i)
    y.append(timeit.timeit(statements[i], setup, number=1))

# Plot
plt.plot(x, y)
plt.xlabel('string length')
plt.ylabel('time(sec)')
plt.show()