以下是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())
无法成功匹配,输出以下错误信息:
Traceback (most recent call last): File "C:/Users/myUser/Documents/myScript.py", line 5, in if procLine.group() == "": AttributeError: 'NoneType' object has no attribute 'group'
当我用 .* 替换正则表达式时,它可以正常工作,这表明正则表达式存在错误。然而,在 https://regex101.com/ 上测试我的 Python 语法的正则表达式和字符串似乎匹配良好。
有什么想法吗?
search
函数,可以查看文档: "请注意,以正面回顾断言开头的模式无法匹配正在搜索的字符串的开头;您最好使用search()
函数而不是match()
函数"。 - niemmi