2

I have a list of expressions and I want to replace each one of those expressions in a file.

I tried this code

for a in ex:
   if a in file.split():
       file = file.replace(a, '[' + ' ' + a + ' ' +']')
print file

My code replaces also the expressions which is part of another expression between brackets. So what I want is to replace only the expressions which are not part of another expression between brackets. How can I get the desired results?

1 Answer 1

5

You could do this through re module. Here the order of the pattern is very important. Since 'organizations of human rights' located before 'human rights', regex engine will try to find organizations of human rights this string on very first. If it finds a match then it will replace the match with [ + match + ]. Then it moves on to the next pattern, that is human rights whether the match is found or not by the previous pattern. Now this human rights pattern will match all the human rights strings which was not present within the organizations of human rights string. Because regex by default won't do an overlapping match. If you want the regex pattern to do an overlapping match, then you need to put the pattern inside lookarounds and the pattern must be surrounded by () (ie, capturing group).

>>> ex = ['liberty of freedom', 'liberty', 'organizations of human rights', 'human rights']
>>> file = " The american people enjoys a liberty of freedom and there are many international organizations of human rights."
>>> reg = '|'.join(ex)
>>> import re
>>> re.sub('('+reg+')', r'[\1]', file)
' The american people enjoys a [liberty of freedom] and there are many international [organizations of human rights].'
Sign up to request clarification or add additional context in comments.

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.