adele adele - 3 months ago 21
Python Question

Using re.sub capture groups

Apologies for the simple question but Im struggling with this:

str = 'EURGBP'

print (re.sub(r'\EUR(GBP)', r'\1', str))


returns

GBP


but

print (re.sub(r'\(EUR)(GBP)', r'\2,\1', str))


gives me
error: unbalanced parenthesis at position 5


I am simply trying to capture both elements enclosed in parenthesis and print them out in reversed order using re like this:

GBPEUR


Can someone please show me what I am doing wrong?

Answer

Don't escape the paren as \( means you are literally searching for a ( in the string so the closing ) has no matching opening (, escaping the E worked as you were looking for the literal E, also just use r'\2\1' unless you want them also separated by a comma:

In [1]: import re

In [2]: s = 'EURGBP'

In [3]: re.sub(r'(EUR)(GBP)', r'\2\1', s)
Out[3]: 'GBPEUR'