在python中看后面

时间:2013-06-16 16:51:14

标签: python regex

假设我有:

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

我希望匹配数字前面的每个单词。

我试过了:

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

但它不起作用。

1 个答案:

答案 0 :(得分:4)

>>> s = "2 dogs. 4 cats. horses. 7 goats"
>>> import re
>>> re.findall(r'\d+\s(\w+)', s)
['dogs', 'cats', 'goats']