2010-04-28 77 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'] 
相关问题