Words do not begin with numbers

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

I tried:

"(?!\d)\w+"

but he still accepts three and eight. I do not want it.

+3
source share
4 answers

Try

\b[a-zA-Z]\w*
+4
source

because it \wincludes a number. you need to do the following:

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

In addition, what you use (?!...)is called negative, but you probably mean negative appearance (?<!...), which, of course, will fail due to the above problem.

eta​​strong > : :

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

:

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

regexp .

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

Source: https://habr.com/ru/post/1743184/


All Articles