提取可选数量的单词
Extracting optional quantity of words
我有这样的字符串-
string="John has got 6 cats but I think my friend Susan has 3 dogs and Mike has 8 fishes"
我想编写正则表达式来提取它后面的模式名称动词和#of 宠物以及它是什么种类的宠物。
re.findall('[A-Za-z]+ \w+ \d+ \w+', string)
适用于 Susan has 3 dogs, Mike has 8 fishes
。
但它不适用于 John has got 6 cats
如何编辑代码以使其在名称后查找一两个词?
提前致谢!
您可以使用非捕获组 (?:
...)
和 {1,2}
来表示 1 到 2 次重复,如下所示
import re
string="John has got 6 cats but I think my friend Susan has 3 dogs and Mike has 8 fishes"
found=re.findall(r'[A-Z][a-z]+ (?:\w+ ){1,2}\d+ \w+', string)
print(found)
输出
['John has got 6 cats', 'Susan has 3 dogs', 'Mike has 8 fishes']
我有这样的字符串-
string="John has got 6 cats but I think my friend Susan has 3 dogs and Mike has 8 fishes"
我想编写正则表达式来提取它后面的模式名称动词和#of 宠物以及它是什么种类的宠物。
re.findall('[A-Za-z]+ \w+ \d+ \w+', string)
适用于 Susan has 3 dogs, Mike has 8 fishes
。
但它不适用于 John has got 6 cats
如何编辑代码以使其在名称后查找一两个词?
提前致谢!
您可以使用非捕获组 (?:
...)
和 {1,2}
来表示 1 到 2 次重复,如下所示
import re
string="John has got 6 cats but I think my friend Susan has 3 dogs and Mike has 8 fishes"
found=re.findall(r'[A-Z][a-z]+ (?:\w+ ){1,2}\d+ \w+', string)
print(found)
输出
['John has got 6 cats', 'Susan has 3 dogs', 'Mike has 8 fishes']