输出:
[----------] My test environment
[==========] 14 tests from 6 test cases ran
[ PASSED ] 14 tests.\
(f, code, output) = entry
if output.find("My test environment) != -1:
for line in output.split("\n"):
m = re.search("PASSED. *\] (\d+) tests", line)
if m:
total = total + int(m.group(1))
在这种情况下,从正在从元组读取的输出(字符串)中读取行,在这种情况下,m似乎始终为none。任何人都可以告诉我正则表达式有什么问题吗?当行被硬编码为字符串或从文件中读取行时,此正则表达式可正常工作。
答案 0 :(得分:0)
for line in output.split("\n"):
m = re.search("PASSED. *\] (\d+) tests", line)
if m:
total = total + int(m.group(1))
答案 1 :(得分:0)
您是否真的需要遍历这些行,您可以使用re.MULTILINE
:
m = re.search("PASSED\s+\] (\d+) tests", output, re.MULTILINE)