2012-07-18 14 views

回答

5
print re.findall(r'\W+', DATA) # note, UPPER-case "W" 

得到你正在尋找名單:

[', ', ' - ', ' ', ' ', ' ', ' ', '!?'] 

我用\W+而不是\w+它否定你正在使用的字符類。

\w Matches word characters, i.e., letters, digits, and underscores. 
    \W Matches non-word characters, i.e., the negated version of \w 

Regular Expression Reference Sheet可能會選擇最好的字符類/元字符爲正則表達式搜索/比賽很有幫助。另外,看到這個tutorial瞭解更多信息(ESP朝頁面底部的參考部分)

3

關於使用互補的正則表達式來\w\W如何?另外,不要單獨列出,而是一次完成全部更有效。 (儘管當然這取決於你打算用它做什麼)

>>> re.findall(r'(\w+)(\W+)', DATA) 
[('Hey', ', '), ('you', ' - '), ('what', ' '), ('are', ' '), ('you', ' '), ('doing', ' '), ('here', '!?')] 

如果你真的想單獨列出,只是壓縮它:

>>> zip(*re.findall(r'(\w+)(\W+)', DATA)) 
[('Hey', 'you', 'what', 'are', 'you', 'doing', 'here'), (', ', ' - ', ' ', ' ', ' ', ' ', '!?')] 
0

re .split

import re 
DATA = "Hey, you - what are you doing here!?" 
print re.split(r'\w+', DATA) 
#prints ['', ', ', ' - ', ' ', ' ', ' ', ' ', '!?'] 

您可能還想過濾掉空字符串以匹配您要求的內容。