Reputation: 111
So I have the following file f1:
---
Proj: pgm1
Status: success
summary: 17 passed, 17 warnings in 18.73s
---
Proj: pgm2
Status: success
summary: 28 passed, 28 warnings in 5.16s
---
Proj: pgm3
Status: failed
summary: 1 failed, 63 passed, 32 warnings in 8.72s
---
I need to find a regex pattern that returns just the "s" after the float value (i.e 8.72s), and then another regex expression that substitutes the "s" for " seconds"
I have this regex pattern to match the whole float number and letter: "\b\d+\.\d+[a-z]"
.
I also have this expression for the substitution: re.sub(r"pattern", " seconds", string)
but again, I'm missing the right pattern to match just the letter "s" after the float value.
Any suggestions on how to make it only match the letter "s" instead of the whole "8.72s" so I can substitute it with " seconds" afterwards, so the file looks like this at the end?:
---
Proj: pgm1
Status: success
summary: 17 passed, 17 warnings in 18.73 seconds
---
Proj: pgm2
Status: success
summary: 28 passed, 28 warnings in 5.16 seconds
---
Proj: pgm3
Status: failed
summary: 1 failed, 63 passed, 32 warnings in 8.72 seconds
---
Upvotes: 0
Views: 69
Reputation: 28983
Any suggestions on how to make it only match the letter "s"
This would be done with a regex lookaround in other languages, so (?<=\d+)s
would match an as after some digits and only replace the "s". Here this approach is difficult because Python's regex engine can't handle that.
Instead, it's easier to identify the numbers and the letter, capture the numbers and put them back into the replacement string.
e.g.
>>> text = "summary: 17 passed, 17 warnings in 18.73s"
>>> re.sub(r"(\b\d+\.\d+)[a-z]", r"\1 seconds", text)
'summary: 17 passed, 17 warnings in 18.73 seconds'
The ()
makes a capture group, and \1
puts the contents of capture group 1 into the replacement string.
Upvotes: 0
Reputation: 521178
Using re.sub
on the pattern \b\d+(?:\.\d+)?s\b
should work here:
inp = """---
Proj: pgm1
Status: success
summary: 17 passed, 17 warnings in 18.73s
---
Proj: pgm2
Status: success
summary: 28 passed, 28 warnings in 5.16s
---
Proj: pgm3
Status: failed
summary: 1 failed, 63 passed, 32 warnings in 8.72s
---"""
output = re.sub(r'\b(\d+(?:\.\d+)?)s\b', r'\1 seconds', inp)
print(output)
The seconds term appear correct if you run the above script.
Upvotes: 1