2013-08-04 42 views
0

我有一個字符串,可以是:如何使用re.findall或re.search讓比賽沒有找到

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

可以通過使用're.IGNORECASE'簡化'[NN] [EE] [WW]'部分很多(也說明're.I')。當然這會影響整個模式,但在這種情況下,這很好,因爲沒有其他字母字符。 – torek

回答

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 ' 
+0

這太棒了!謝謝! –