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

时间:2021-03-09 22:37:46

I have a string that can be:

我有一个字符串可以是:

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

I need to get:

我需要得到:

result = 'Something Else  other and another  with  '

But the best I've achieved so far is:

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

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

How to using findall to get the part of the string NOT matching the searching pattern?

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

1 个解决方案

#1


3  

re.sub:

应用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  '

#1


3  

re.sub:

应用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  '