如何使用re.findall或re.search来获取未找到的匹配项

时间:2013-08-04 10:09:32

标签: python regex string

我有一个字符串可以是:

test = 'Something Else ( neW ) other and another (nEw ) with (nEw ) '

我需要得到:

result = 'Something Else  other and another  with  '

但到目前为止我所取得的最好成绩是:

 import re
 f = re.findall(ur'\(\s*[nN][eE][wW]\s*\)',test)
 for i in f: test = test.replace(i,'')

如何使用findall来获取与搜索模式不匹配的字符串部分?

1 个答案:

答案 0 :(得分:3)

re.sub

>>> import re
>>> test = 'Something Else ( neW ) other and another (nEw ) with (nEw ) '
>>> re.sub(r'\(\s*[nN][eE][wW]\s*\)','',test)
'Something Else  other and another  with  '