Python正则表达式与Regex101的区别

4

输入字符串:

I0419 01:52:16.606123 136 TrainerInternal.cpp:181] Pass=15 Batch=74 samples=3670 AvgCost=263.331 Eval: classification_error_evaluator=0.970178 I0419 01:52:16.815407 136 Tester.cpp:115] Test samples=458 cost=203.737 Eval: classification_error_evaluator=0.934446

匹配模式:

Pass=([0-9]+).*classification_error_evaluator=(0.[0-9]+).*classification_error_evaluator=(0.[0-9]+)

期望输出结果:

(15, 0.970178, 0.934446)

在Regex101(https://regex101.com/r/Hwxsib/1)上,看起来我捕获了正确的模式。

但是在Python中,它没有匹配到任何组并且什么都没有捕获:

import re

x = "I0419 01:52:16.606123   136 TrainerInternal.cpp:181]  Pass=15 Batch=74 samples=3670 AvgCost=263.331 Eval: classification_error_evaluator=0.970178 I0419 01:52:16.815407   136 Tester.cpp:115]  Test samples=458 cost=203.737 Eval: classification_error_evaluator=0.934446"

pattern = "Pass=([0-9]+).*classification_error_evaluator=(0\.[0-9]+).*classification_error_evaluator=(0\.[0-9]+)"

re.match(pattern, x)

正则表达式101的设置与Python中的re包有何区别?它们是相同的吗? 它们是否具有不同的标志或设置/其他内容?

为什么在Python中没有匹配模式?


2
你可能需要使用re.search,而re.match只会在字符串开头匹配成功。 - anthony sottile
啊...re.findall(pattern, x)[0] - alvas
2
实际上,这里是生成的代码:https://regex101.com/r/Hwxsib/1/codegen?language=python - anthony sottile
哦,太酷了!没想到 regex101 上有这个函数! - alvas
2个回答

6
您可能需要使用re.search,因为re.match仅在字符串开头出现时返回匹配项。
regex101还会显示它所使用的代码:https://regex101.com/r/Hwxsib/1/codegen?language=python 从regex101的代码中,这是它正在做的事情(摘录并编辑):
import re

regex = r"..."

test_str = "..."

matches = re.finditer(regex, test_str)

...

3
你想要使用re.search。如果匹配结果不在字符串开头,match函数将无法返回任何结果!
import re

x = "I0419 01:52:16.606123   136 TrainerInternal.cpp:181]  Pass=15 Batch=74 samples=3670 AvgCost=263.331 Eval: classification_error_evaluator=0.970178 I0419 01:52:16.815407   136 Tester.cpp:115]  Test samples=458 cost=203.737 Eval: classification_error_evaluator=0.934446"

pattern = r'Pass=([0-9]+).*classification_error_evaluator=(0\.[0-9]+).*classification_error_evaluator=(0\.[0-9]+)'

print re.search(pattern, x).groups(1)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接