下面是我的输入文件
input.txt
min=1310ns median=1344ns max=1399ns first=2280ns
min=1293ns median=1331ns max=18400ns first=2284ns
min=1277ns median=1302ns max=1346ns first=2363ns
我的python代码
import re
input_file = open("input.txt", "r")
output_file = open("output.data", "w")
for line in input_file:
match_defines = re.match(r'\s*min=([0-9]+)', line) # works fine
match_defines = re.match(r'\s*min=([0-9]+) median=([0-9]+) max=([0-9]+) first=([0-9]+)', line) # this doesn't work.
if match_defines:
newline1= "\n %s\t%s\t%s\t%s\n" % (match_defines.group(1), match_defines.group(2), match_defines.group(3), match_defines.group(4))
output_file.write(newline1)
else:
output_file.write(line)
我的预期结果是
1310 1344 1399 2280
1293 1331 18400 2284
1277 1302 1346 2363
我如何修改我的正则表达式来得到这个
谢谢你的回答
您忘记在正则表达式中添加
ns
:见regex demo
我建议使用命名的捕获组来更容易地访问捕获,并且可能使用
\s+
而不是文字空间:见another demo
相关问题 更多 >
编程相关推荐