问题描述:

Suppose I have:

string = "2 dogs. 4 cats. 9 horses. 7 goats"

I want to match every word that precedes a number.

I tried:

matches = re.search(r"(?<=\d+) \w+", string)

but it isn't working.

网友答案:
>>> s = "2 dogs. 4 cats. horses. 7 goats"
>>> import re
>>> re.findall(r'\d+\s(\w+)', s)
['dogs', 'cats', 'goats']
相关阅读:
Top