如何查找以ing结尾的单词 [英] How to find words ending with ing

查看:43
本文介绍了如何查找以ing结尾的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找以 ing 结尾的单词并打印它们,我当前的代码打印的是 ing 而不是单词.

#匹配所有以ing结尾的单词进口重新expression = input("请输入一个表达式:")打印(re.findall(r'\b\w+(ing\b)', 表达式))

所以如果我们输入一个表达式:分享你听到的所有信息

我想打印['sharing', 'hearing']相反,我有 ['ing', 'ing'] 打印出来

有没有快速解决的方法?

解决方案

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

<预><代码>>>>s="分享你听到的所有信息">>>re.findall(r'\b(\w+ing)\b',s)['分享','聆听']

你也可以在列表推导中使用 str.endswith 方法:

<预><代码>>>>[w for w in s.split() if w.endswith('ing')]['分享','聆听']

I am looking to find words ending with ing and print them, my current code prints out ing instead of the word.

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

so if we enter as an expression : sharing all the information you are hearing

I would like ['sharing', 'hearing'] to be printed out instead I am having ['ing', 'ing'] printed out

Is there a quick way to fix that ?

解决方案

Your capture grouping is wrong try the following :

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

Also you can use str.endswith method within a list comprehension :

>>> [w for w in s.split() if w.endswith('ing')]
['sharing', 'hearing']

这篇关于如何查找以ing结尾的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆