0

I need to replace every parenthesis at the end of a string. I use this code:

a = '1 (FR) Product (IT, DE, ES)'
b = re.sub(r' \((.*?)\)',r'', a)

But this will replace every parenthesis in my string. How do I tell python to replace it only if the pattern is at the end of the string?

2 Answers 2

2

Capture everything but ( before ) and end of string identifier $:

>>> import re
>>> a = '1 (FR) Product (IT, DE, ES)'
>>> re.sub(r'\(([^(]*\))$', '', a)
'1 (FR) Product '
Sign up to request clarification or add additional context in comments.

Comments

0

if a[-1] == ")" a = a[:-2] will work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.