python 属性错误:“NoneType”对象在regEx中没有属性“group”

x33g5p2x  于 2023-06-28  发布在  Python
关注(0)|答案(1)|浏览(133)

代码

import re

string1 = """The Euro STOXX 600 index, which tracks all stock markets across Europe including the F
         TSE, fell by 11.48% – the worst day since it launched in 1998. The panic 1334 selling prompted by the coronavirus has wiped £2.7tn off the value of STOXX 600 shares since its all-time peak on 
         19 February."""

s8=re.search(r".+\s(.+ex).+\s(\d\d\s.+).",string1)

s8.groups()

print(s8)

预计将打印(“索引”,“2月19日”)

错误

s8.groups()
    ^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'groups'

有谁能告诉我为什么会出现这个错误吗?

ergxz8rk

ergxz8rk1#

默认情况下,.不匹配行尾,因此在输入字符串中找不到匹配项,s8最终为空。
您可以通过传递re.DOTALL标志来更改该行为:

s8 = re.search(r".+\s(.+ex).+\s(\d\d\s.+).", string1, flags=re.DOTALL)

相关问题