开发者

Words doesn't starts with numbers

I have a string "one two 9three 52eight four", so I only want to get "one two four", because "three" starts with "9" and "eight" starts with "52".

I tr开发者_如何学JAVAied:

"(?!\d)\w+"

but it's still taking the "three" and "eight". I don't want it.


Try

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


that's because \w includes number. what you need to do is:

>>> s = "one two 9three 52eight four"
>>> import re
>>> re.findall(r'\b[a-z]+\b', s, re.I)
['one', 'two', 'four']

Also, what you're using (?!...) is called negative look-ahead, while you probably meant negative look-behind (?<!...), which would of course still fail because of above-mentioned issue.

eta: then you just need a single word border:

>>> re.findall(r'\b(?!\d)\w+', s)
['one', 'two', 'four']


Works fine for me:

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

Prints:

['one', 'two', 'four']


regexp might be overkill.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜