我正在解析一些日志,并想提取某种类型的所有参数名称。为简单起见,我将只包含一个小的子字符串。
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)
>> ['=','=','=']
最佳答案
findall返回所有groups
。您有一个(=)
组,因此将其删除。
regex = r'(?s)WT.+?(?==)'
^^^^^
同样也不需要
lookahead
。输出:
['WT.tz', 'WT.bh', 'WT.ul']
关于python - Python regex group()可以工作,但是findall()的结果与我预期的不一样,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33021805/