输入字符串:
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中没有匹配模式?
re.search
,而re.match
只会在字符串开头匹配成功。 - anthony sottilere.findall(pattern, x)[0]
- alvas