1

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)
>> ['=','=','=']

2 Answers 2

2

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']

Sign up to request clarification or add additional context in comments.

Comments

0
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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.