以下python代码:
import re
line="http://google.com"
procLine = re.match(r'(?<=http).*', line)
if procLine.group() == "":
print(line + ": did not match regex")
else:
print(procLine.group())
Run Code Online (Sandbox Code Playgroud)
匹配不成功,输出如下错误:
回溯(最近一次调用):文件“C:/Users/myUser/Documents/myScript.py”,第 5 行,如果 procLine.group() ==“”:AttributeError: 'NoneType' 对象没有属性 'group '
当我只用 .* 替换正则表达式时,它工作正常,这表明它是错误的正则表达式,但是,在https://regex101.com/ 上,当我测试我的正则表达式和 python 风格的字符串时,它似乎匹配得很好。
有任何想法吗?