Search code examples
pythonreplaceasteriskparentheses

Asterisk character in Python with parentheses and spaces


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.


Solution

  • You should use regular expressions for this, take a look at: https://docs.python.org/3/library/re.html, more specific, you want the sub method.

    I think this is what you want (untested):

    import re
    string = 'Hello ) world Reply ( some text ) and some Reply ( more text ) and that is ) it.'
    new_string = re.sub(r"Reply \((.+?)\)",lambda a : a.group(1), string)
    

    Give it a try and let me know!

    Some explanation:

    The first parameter of the sub method is the pattern we want to find and substitute, so r"Reply \((.+?)\)" matches the pattern you want, Reply followed by something between parenthesis, also note that we capture the thing between parenthesis. Also notice the ungreedy operator (?).

    The second parameter is a lambda function to generate what is going to replace the pattern, notice that it receives as argument the match object. So we use the captured data by returning group 1 of the match object.

    The third parameter is the string we want to search and substitute things at.