该
AttributeError: 'NoneType' object has no attribute'group'错误仅表示您没有匹配项,并尝试访问空对象的组内容。
我认为最简单的方法是遍历搜索匹配项的列表项,找到后,获取组1的内容并将其分配给
value:
import relist = ['firstString','xxxSTATUS=100','thirdString','fourthString']value = ""for x in list: m = re.search('STATUS=(.*)', x) if m: value = m.group(1) breakprint(value)请注意,您不需要
.*模式中的首字母,因为
re.search模式不会锚定在字符串的开头。
参见Python演示
另外,如果您想使用最初的方法,则需要先使用来检查是否存在匹配项
if re.search('STATUS=(.*)',x),然后再次使用进行运行以获取组内容re.search('STATUS=(.*)', x).group(1):value = next(re.search('STATUS=(.*)', x).group(1) for x in list if re.search('STATUS=(.*)', x))


