I'm modifying an array of strings by applying a series of conversions to them. One of the conversions is to swap a country name in parentheses with the full name of that country. For example:
Foo (GB) -> Foo Great Britain
I have an array of conversion objects of the form:
conversion.input_str = '(GB)'
conversion.output_str = 'Great Britain'
And I'm trying to do it using regex, as follows:
for conv in conversions:
match_expr = re.compile(rf'(\s|\b){conv.input_str}(\s|\b)')
converted_str = re.sub(match_expr, conv.output_str, str_to_convert)
The \s and \b are there to prevent matches with word internal strings. All my conversions work except the parentheses one.
If I use the plain conv.input_str
, I get output of Foo ()
If I try escaping using re.escape(conv.input_str)
this seems to create the right match_expr but gives me Foo(GB).
What am I doing wrong?