Reputation: 3
I'm struggling with regular expressions.
Here is an example string:
(1-3)+XD(4-18):SP(19-36)+(37-96)
How would I extract the digits from the parentheses that are not preceded by SP or XD so that my result would be:
[1-3, 37-96]
Right now I am trying something like:
re.findall(r'[:|\+]\([0-9]+-[0-9]+\)', '(1-3)+XD(4-18):SP(19-36)+(37-96)')
But this returns:
['+(37-96)']
It misses the first (1-3) capture (because I cannot figure it out) and I also have to then operate on the substring returned from the match to extract the digits.
Is there a better way to do this?
Upvotes: 0
Views: 152
Reputation: 781096
Use a negative lookbehind to exclude SP
and XD
, rather than explicitly matching :
and +
.
And if you don't want the results to include the parentheses, put a capture group around the numbers. Then re.findall()
only returns the capture groups, not the matches.
re.findall(r'(?<!SP|XD)\((\d+-\d+)\)', '(1-3)+XD(4-18):SP(19-36)+(37-96)')
Upvotes: 1