How to use regular expressions to match digits inside parentheses?

Question:

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?

Asked By: unbutu

||

Answers:

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)')

DEMO

Answered By: Barmar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.