How to find words ending with ing

I am looking to find words ending with ing and type them, my current code prints 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 introduce the expression: sharing all the information you are hearing

I would like to print ['sharing', 'hearing'] instead I have ['ing', 'ing'] printed

Is there a quick way to fix this?

+5
source share
3 answers

Your capture grouping is incorrect, try the following:

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

You can also use the str.endswith method in list comprehension:

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

Brackets β€œcapture” text from your string. You have '(ing\b)' , so only ing is fixed. Move the open parenthesis so that it spans the entire line you want: r'\b(\w+ing)\b' . See if that helps.

+4
source
 sentence = 'sharing all the information you are hearing' # spit so we have list of words from sentence words = sentence.split(' ') ending_with('ing',words) def ending_with(ending, words): # loop through words for word in words: # if word has ends with ending if word.endswith(ending): # print print word 
0
source

All Articles