匹配所有以s结尾的单词

Moz*_*ein 1 python regex python-3.x

我有一个编写正则表达式的程序,以匹配最后一个字母以's'结尾的所有单词.然而,我遇到的问题是,它只匹配第一个单词,然后停止.所以,如果我输入表达式:"詹姆斯对我们很好"

我预计

 matchObj.group() : 'james'
 matchObj.group(1): 'is'
 matchObj.group(2): 'us'
Run Code Online (Sandbox Code Playgroud)

但是我只得到 matchObj.group() : 'james'

我相信问题可以追溯到匹配函数的工作方式,是否有解决方法?这是我的代码

import re



matchObj = re.match(r'\w+s', expression, re.M|re.I)

print("matchObj.group() : ", matchObj.group())
print("matchObj.group(1) : ", matchObj.group(1))
print("matchObj.group(2) : ", matchObj.group(2))
Run Code Online (Sandbox Code Playgroud)

Mal*_*imi 5

您需要re.findall像这样使用以匹配包含字符串的所有对象:

words = re.findall(r'\b\w+s\b', your_string)
Run Code Online (Sandbox Code Playgroud)