2010-04-28 101 views
3

我有一個字符串「one two 9three 52eight four」,所以我只想得到「one two four」,因爲「three」以「9」開始,「eight」以「 52" 。單詞不以字母開頭

我想:

"(?!\d)\w+" 

,但它仍然以 「三化」 和 「八」。我不想要它。

回答

4

嘗試

\b[a-zA-Z]\w* 
+0

謝謝。這一個正在工作。 – pocoa 2010-04-28 14:18:14

+0

這絕對是正確的答案+1包括小寫字母和大寫字母 – ant 2010-04-28 14:19:10

+0

@ c0mrade:並且哪個答案不是? – SilentGhost 2010-04-28 14:21:31

1

正常工作對我來說:

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

奇怪,看看這個http://tinyurl.com/2ctzevm – pocoa 2010-04-28 14:19:59

+0

@pocoa,因爲他分裂成字第一,並通過與're.match'字這就需要匹配在開始時檢查一個字的字符串。這就是爲什麼9three和52eight不匹配。 – YOU 2010-04-28 14:29:42

+0

@ S.Mark我沒有在這臺公用計算機上安裝Python。所以我依靠在線工具。對不起@英里82! – pocoa 2010-04-28 14:36:16

2

這是因爲\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'] 
+0

謝謝。對不起,我沒有提供足夠的信息。如果它與星號相符但我不想匹配,但「four8」沒問題。 – pocoa 2010-04-28 14:17:44

+0

謝謝,第二個例子也在工作。 – pocoa 2010-04-28 14:24:22

0

正則表達式可能是矯枉過正。

In [3]: [word for word in eg.split(' ') if not word[0].isdigit()] 
Out[3]: ['one', 'two', 'four']