我有一個字符串「one two 9three 52eight four」,所以我只想得到「one two four」,因爲「three」以「9」開始,「eight」以「 52" 。單詞不以字母開頭
我想:
"(?!\d)\w+"
,但它仍然以 「三化」 和 「八」。我不想要它。
我有一個字符串「one two 9three 52eight four」,所以我只想得到「one two four」,因爲「three」以「9」開始,「eight」以「 52" 。單詞不以字母開頭
我想:
"(?!\d)\w+"
,但它仍然以 「三化」 和 「八」。我不想要它。
嘗試
\b[a-zA-Z]\w*
正常工作對我來說:
import re
l = "one two 9three 52eight four".split()
c = re.compile("(?!\d)\w+")
m = [w for w in l if re.match(c, w)]
print m
打印:
['one', 'two', 'four']
這是因爲\w
包括數。你需要做的是:
>>> s = "one two 9three 52eight four"
>>> import re
>>> re.findall(r'\b[a-z]+\b', s, re.I)
['one', 'two', 'four']
而且,你正在使用(?!...)
稱爲負先行,而你可能是指負向後看(?<!...)
,這當然會仍然失敗,因爲上面提到的問題。
ETA:那麼你只需要一個字邊界:
>>> re.findall(r'\b(?!\d)\w+', s)
['one', 'two', 'four']
正則表達式可能是矯枉過正。
In [3]: [word for word in eg.split(' ') if not word[0].isdigit()]
Out[3]: ['one', 'two', 'four']
謝謝。這一個正在工作。 – pocoa 2010-04-28 14:18:14
這絕對是正確的答案+1包括小寫字母和大寫字母 – ant 2010-04-28 14:19:10
@ c0mrade:並且哪個答案不是? – SilentGhost 2010-04-28 14:21:31