[英]Python Regex multi group pattern findall() returning empty list
I am trying to use Regex multi group pattern to extract different CPU specs from a line, but getting an empty list.When i try individual groups separately, i am able to extract corresponding values. 我正在尝试使用Regex多组模式从一行中提取不同的CPU规格,但得到一个空列表。当我分别尝试单个组时,我能够提取相应的值。 How should i use multi group pattern here?
我应该如何在这里使用多组模式? Please help!
请帮忙!
import re
line = "R7000 CPU at 160MHz, Implementation 39, Rev 2.1, 256KB L2, 512KB L3 Cache"
pat_cpu_values_combined = r"(?P<freq>\s+\w+Hz)(?P<L2>\s+\w+\s+L2)(?P<L3>\s+\w+\s+L3)"
pat_cpu_freq = r"(?P<freq>\s+\w+Hz)"
pat_cpu_l2 = r"(?P<L2>\s+\w+\s+L2)"
pat_cpu_l3 = r"(?P<L3>\s+\w+\s+L3)"
# empty list coming when pat_cpu_values_combined is searched
print re.findall(pat_cpu_values_combined, line)
# below individual group pattern findall are working fine
print re.findall(pat_cpu_freq, line)
print re.findall(pat_cpu_l2, line)
print re.findall(pat_cpu_l3, line)
Your combined regex is looking for each of those patterns smashed together, with no intermediate characters. 您的组合正则表达式正在寻找每个捣碎在一起的模式,而没有中间字符。 You can instead combine your patterns with the
|
您可以将模式与
|
组合使用|
separator. 分隔器。
pat_cpu_values_combined = r"(?P<freq>\s+\w+Hz)|(?P<L2>\s+\w+\s+L2)|(?P<L3>\s+\w+\s+L3)"
[''.join(g) for g in re.findall(pat_cpu_values_combined, line)]
# returns:
[' 160MHz', ' 256KB L2', ' 512KB L3']
When you combine them, you are not accounting for the characters in between the things you want to match. 当您将它们组合在一起时,您并没有考虑要匹配的事物之间的字符。 Try using this for your combined regex:
尝试将其用于组合的正则表达式:
(?P<freq>\s+\w+Hz).*?(?P<L2>\s+\w+\s+L2).*?(?P<L3>\s+\w+\s+L3)
pat_cpu_values_combined
expects strings matching your three individual patterns to occur with nothing in between them. pat_cpu_values_combined
期望与您的三个单独模式匹配的字符串不会出现在它们之间。
If you want to find all three in that order, use something like: 如果要按此顺序查找所有三个,请使用类似以下内容的方法:
pat_cpu_values_combined = r"(?P<freq>\s+\w+Hz).*?(?P<L2>\s+\w+\s+L2).*?(?P<L3>\s+\w+\s+L3)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.