I want to search for a certain string in a file that is followed by another string.
I have the following string:
string = 'Hello ) world Reply ( some text ) and some Reply ( more text ) and that is ) it.'
and I want to have the string replaced with
'Hello ) world some text and some more text and that is ) it.'
So basically, I want the Reply
and the parentheses gone, but I want to keep whatever is inside the parentheses and I want to keep parentheses that do not follow a Reply (
. I have tried to use the answers provided here, but they give me trouble when I'm trying to use parentheses and spaces. I've tried to do this with the following code:
string = 'Hello world Reply ( some text ) and some Reply ( more text ) and that is it.'
find = '* Reply ( * ) *'
replace = '* * *'
for f,r in zip(find.strip('*').split('*'), replace.strip('*').split('*')):
string = string.replace(f,r)
print string
Hello world some text and some more text and that is it.
Unfortunately, this removes all parentheses. As you can imagine, nested parentheses cause problems when you only want to remove one pair of parentheses, and all closing parentheses are removed.
Is there a way to do this task without removing all closing parentheses? If you need any more info, please let me know.
Any help is greatly appreciated.