Apologies for the simple question but Im struggling with this:
str = 'EURGBP'
print (re.sub(r'\EUR(GBP)', r'\1', str))
print (re.sub(r'\(EUR)(GBP)', r'\2,\1', str))
error: unbalanced parenthesis at position 5
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 : import re In : s = 'EURGBP' In : re.sub(r'(EUR)(GBP)', r'\2\1', s) Out: 'GBPEUR'