替换文件中的字符串

sss*_*sss 2 python python-2.7 python-3.x

我有一个表达式列表,我想替换文件中的每个表达式.

我试过这段代码

for a in ex:
   if a in file.split():
       file = file.replace(a, '[' + ' ' + a + ' ' +']')
print file
Run Code Online (Sandbox Code Playgroud)

我的代码也替换了括号中另一个表达式的表达式.所以我想要的是只替换括号中不属于另一个表达式的表达式.如何获得理想的结果?

Avi*_*Raj 5

你可以通过re模块做到这一点.这里模式的顺序非常重要.由于'organizations of human rights'之前的位置'human rights',正则表达式引擎会尝试首先找到organizations of human rights这个字符串.如果找到匹配,那么它将用[+ match + 替换匹配].然后它继续前进到下一个模式,即前一个模式human rights 是否找到匹配.现在,此human rights模式将匹配human rights字符串中不存在的所有organizations of human rights字符串.因为默认情况下正则表达式不会重叠匹配.如果您希望正则表达式模式执行重叠匹配,则需要将模式置于外观中,并且模式必须被()(即捕获组)包围.

>>> 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].'
Run Code Online (Sandbox Code Playgroud)