Reputation: 5282
I'm parsing some logs and want to extract all parameter names of a certain type. For simplicity, I'll just include a small substring.
log = 'WT.tz=-8&WT.bh=23&WT.ul=en-US'
#I want to find all strings that start with WT and get WT and all the following characters until I find an & or the end of the string. I tested this on an online regex page and it seems to work great.
regex = r'(?s)(?=WT).+?(?=(=))'
# if I try to find the first I get what I expected
re.search(regex,log).group()
>> 'WT.tz'
#when I try to find all I do not get what I thought I was going to get.
re.findall(regex,log)
>> ['=','=','=']
Upvotes: 1
Views: 215
Reputation: 5658
log = 'WT.tz=-8&WT.bh=23&WT.ul=en-US'
print(re.findall(r'WT\.[^&]*\b',log))
['WT.tz=-8', 'WT.bh=23', 'WT.ul=en-US']
Upvotes: 0
Reputation: 67968
findall returns all the groups
.You have a group (=)
.So remove it.
regex = r'(?s)WT.+?(?==)'
^^^^^
Also there is no need for lookahead
.
Output: ['WT.tz', 'WT.bh', 'WT.ul']
Upvotes: 2