如何找到以ing结尾的单词

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

我希望找到以ing结尾的单词并打印出来,我当前的代码打印出而不是单词.

#match all words ending in ing
import re
expression = input("please enter an expression: ")
print(re.findall(r'\b\w+(ing\b)', expression))
Run Code Online (Sandbox Code Playgroud)

所以如果我们输入一个表达式: sharing all the information you are hearing

我希望['sharing', 'hearing']打印出来,而不是['ing', 'ing']打印出来

有没有快速解决方法?

Kas*_*mvd 11

您的捕获分组错误尝试以下操作:

>>> s="sharing all the information you are hearing"
>>> re.findall(r'\b(\w+ing)\b',s)
['sharing', 'hearing']
Run Code Online (Sandbox Code Playgroud)

您还可以str.endswith在列表解析中使用方法:

>>> [w for w in s.split() if w.endswith('ing')]
['sharing', 'hearing']
Run Code Online (Sandbox Code Playgroud)