user_12
user_12

Reputation: 2119

How to use regex to replace unmatched parentheses in a string with no nested parentheses?

I have a string like this,

text  = 'AWS certification ( AWS Solutions Architect'
text1 = 'AWS certification ( AWS Solutions Architect )'

how to replace ( in text variable but ignore it in text1 because in text1 we have both the parenthesis?

I have tried a basic re.sub('\(', '-', text) but this is replacing ( in both text and text1 which is not what I want.

Any help is appreciated.

Upvotes: 1

Views: 788

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626747

You can use

text = re.sub(r'(\([^()]*\))|[()]', lambda x: x.group(1) or "-", text)

See the Python demo:

import re
texts  = [ 'AWS certification ( AWS Solutions Architect', 'AWS certification ( AWS Solutions Architect )']
for text in texts:
    print( re.sub(r'(\([^()]*\))|[()]', lambda x: x.group(1) or "-", text) )
# => AWS certification - AWS Solutions Architect
#    AWS certification ( AWS Solutions Architect )

Here, (\([^()]*\))|[()] finds and captures into Group 1 any strings between parentheses having no inner parentheses, and then [()] matches either ( or ) in any other context. Then, the replacement is either - if Group 1 was not matched, else, Group 1 value is returned.

Upvotes: 1

Aaj Kaal
Aaj Kaal

Reputation: 1274

Assuming you don't have any other condition, replace only if opening and closing parenthesis' count is not same.

>>> if text.count('(') != text.count(')'):
...   re.sub('\(', '-', text)
...
'AWS certification - AWS Solutions Architect'

Upvotes: 0

Related Questions