Reputation: 5226
let's say we have this:
.................
=== Operation 'abcd::ddca:dsd' ended in 1.234s /1.234s (100.00%) execution time
................
Using notepad++, I am able to identify this with:
^\=* Operation '([\d\D]*)' ended in (\d*.\d*)s\s*/(\d*.\d*)s \([\d\D]*\) execution time
I want the operation name and execution times to be grouped.
In python, trying this:
exp=re.compile(r"^\=* Operation \'([\d\D]*)\' ended in (\d*.\d*)s\s*/(\d*.\d*)s \([\d\D]*\) execution time")
provides nothing.
I've tried \\(
for escaping the literal paranthesis but it didn't work. I'm guessing I don't need to do that since I'm using r"[exp]" when building the object expression.
Any ideas on how to get the same result as in notepad++?
LE: tried only with:
exp=re.compile(r"^\=* Operation \'([\d\D]*)\'", flags=re.MULTILINE)
still doesn't find anything.
LE2:
later in the code I'm using groups=exp.match(INPUT)
and I get the pairs with groups.group(n)
ANSWER:
the problem was match
. Using search
fixed the problem
Upvotes: 2
Views: 472
Reputation: 2382
The regular expression mentioned in the question is working for me without any change.
>>> s = """
... .................
... === Operation 'abcd::ddca:dsd' ended in 1.234s /1.234s (100.00%) execution time
... ................
... """
>>> import re
>>> exp = re.compile(r"^\=* Operation \'([\d\D]*)\' ended in (\d*.\d*)s\s*/(\d*.\d*)s \([\d\D]*\) execution time", flags=re.M)
>>> re.search(exp, s)
<_sre.SRE_Match object at 0x1038766b8>
>>> re.findall(exp, s)
[('abcd::ddca:dsd', '1.234', '1.234')]
Two things to consider though:
re.M
in the flags argumentsearch
or findall
method to do the matching. Make sure you are not using re.match
because it will match only the beginning of the string.Upvotes: 2
Reputation: 174624
Try this - I am not a regex guru, but this worked for me.
>>> i
"=== Operation 'abcd::ddca:dsd' ended in 1.234s /1.234s (100.00%) execution time"
>>> exp = re.compile(r'^={3} Operation (.*?) ended in (\d+\.\d+s)(?:.*?)$')
>>> re.findall(exp,i)
[("'abcd::ddca:dsd'", '1.234s')]
Upvotes: 0
Reputation: 18473
I noticed that you have double quotes at the end of the abcd::ddca:dsd
So :
exp=re.compile(r"^\=* Operation '([\d\D]*)\" ended in (\d*\.\d*)s\s*/(\d*\.\d*)s \([\d\D]*\) execution time", flags=re.MULTILINE)
Upvotes: 1
Reputation: 336128
You need to allow the ^
to match the start of a line, not just the start of the entire string:
exp=re.compile(r"^\=* Operation '([\d\D]*)' ended in (\d*\.\d*)s\s*/(\d*\.\d*)s \([\d\D]*\) execution time", flags=re.MULTILINE)
Upvotes: 0