0

Following is my input file

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

my python code

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)

My expected result is

1310   1344   1399   2280
1293   1331   18400  2284
1277   1302   1346   2363

How do i modify my regex to get this.

Thanks for your answers.

0

1 Answer 1

1

You forgot to add ns in the regex:

\s*min=([0-9]+)ns median=([0-9]+)ns max=([0-9]+)ns first=([0-9]+)
               ^^                ^^             ^^

See regex demo

I suggest using named capture groups to make it easier to access the captures and perhaps use \s+ instead of literal spaces:

\s*min=(?P<min>[0-9]+)ns\s+median=(?P<median>[0-9]+)ns\s+max=(?P<max>[0-9]+)ns\s+first=(?P<first>[0-9]+)

See another demo

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I should have been a bit more careful
Also, I suggest using named captures, they are quite handy in situations when you have more than 2 groups.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.