The AttributeError: 'NoneType' object has no attribute 'group'
error just means you got no match and tried to access group contents of a null object.
I think the easiest way is to iterate over the list items searching for the match, and once found, get Group 1 contents and assign them to value
:
import re
list = ['firstString','xxxSTATUS=100','thirdString','fourthString']
value = ""
for x in list:
m = re.search('STATUS=(.*)', x)
if m:
value = m.group(1)
break
print(value)
Note you do not need the initial .*
in the pattern as re.search
pattern is not anchored at the start of the string.
See the Python demo
Also, if you want your initial approach to work, you need to check if there is a match first with if re.search('STATUS=(.*)', x)
, and then run it again to get the group contents with re.search('STATUS=(.*)', x).group(1)
:
value = next(re.search('STATUS=(.*)', x).group(1) for x in list if re.search('STATUS=(.*)', x))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…