2015-07-28 47 views
0

(新的正則表達式)中的表達式時,我正面臨着難以使用正則表達式操作的不等於。如何在正則表達式中使用已知的

我做這樣的事情:

re.findall("(<.*?>)", "i have a problem in doing <regex> for <not in> how can I do it") 

它給我的輸出,如:

['<regex>', '<not in>'] 

我想要做的正好相反,我所要的輸出是

[('i have a problem in doing'), ('for'), ('how can I do it')] 

但不知何故,我無法解決這個問題,一點幫助將不勝感激。

回答

1

你需要re.split,取而代之的.*?它能夠更好地使用否定的字符類[^<]*拒不比賽外標記:

>>> re.split("<[^<]*>", "i have a problem in doing <regex> for <not in> how can I do it") 
['i have a problem in doing ', ' for ', ' how can I do it'] 
2

使用re.split()代替:

>>> re.split("\s*<[^<]*>\s*", "i have a problem in doing <regex> for <not in> how can I do it") 
['i have a problem in doing', 'for', 'how can I do it'] 

注意,這個刪除了前並根據您所需的輸出結尾拖尾空格字符。

+0

呵呵呵呵!!!!!!!!我非常喜歡這種修改:P – vks

+0

謝謝@vks,我在這裏招待!實際上,我「借用」了卡斯拉姆莫德的模式,以便正確處理諸如「我有一個問題」這樣的刺痛,原始模式無法正常工作。 – mhawke

1
print re.findall("(?:<.*?>)|([^<>]+)", "i have a problem in doing <regex> for <not in> how can I do it") 

你可以嘗試this.If你不想空的結果做

print [i for i in re.findall("(?:<.*?>)|([^<>]+)", "i have a problem in doing <regex> for <not in> how can I do it") if i] 
相關問題