Python regex group()有效,但findall()结果并不像我预期的那样

时间:2015-10-08 16:58:19

标签: python regex

我正在解析一些日志,并希望提取某种类型的所有参数名称。为简单起见,我只需要包含一个小子串。

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 个答案:

答案 0 :(得分:2)

findall会返回所有groups。您有一个组(=)。请将其删除。

regex = r'(?s)WT.+?(?==)'

                   ^^^^^

此外,不需要lookahead

输出:['WT.tz', 'WT.bh', 'WT.ul']

答案 1 :(得分: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']