Replace text between parentheses in python

Viewed 5201

My string will contain () in it. What I need to do is to change the text between the brackets.

Example string: "B.TECH(CS,IT)". In my string I need to change the content present inside the brackets to something like this.. B.TECH(ECE,EEE)

What I tried to resolve this problem is as follows..

reg = r'(()([\s\S]*?)())'
a = 'B.TECH(CS,IT)'
re.sub(reg,"(ECE,EEE)",a)

But I got output like this..

'(ECE,EEE)B(ECE,EEE).(ECE,EEE)T(ECE,EEE)E(ECE,EEE)C(ECE,EEE)H(ECE,EEE)((ECE,EEE)C(ECE,EEE)S(ECE,EEE),(ECE,EEE)I(ECE,EEE)T(ECE,EEE))(ECE,EEE)'

Valid output should be like this..

B.TECH(CS,IT)

Where I am missing and how to correctly replace the text.

2 Answers
Related