假设我有:
string = "2 dogs. 4 cats. 9 horses. 7 goats"
我希望匹配数字前面的每个单词。
我试过了:
matches = re.search(r"(?<=\d+) \w+", string)
但它不起作用。
答案 0 :(得分:4)
>>> s = "2 dogs. 4 cats. horses. 7 goats"
>>> import re
>>> re.findall(r'\d+\s(\w+)', s)
['dogs', 'cats', 'goats']