Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find words ending with 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 ?

like image 386
Mozein Avatar asked Apr 16 '15 16:04

Mozein


People also ask

How do you know if a word ends with ing in Python?

Parentheses "capture" text from your string. You have '(ing\b)' , so only the ing is being captured. Move the open parenthesis so it encompasses the entire string that you want: r'\b(\w+ing)\b' . See if that helps.

What is an action word ending in ing?

For many verbs we make the ING form by simply adding -ING to end of the verb. eat - eating. speak - speaking. cook - cooking. start - starting.


1 Answers

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']
like image 85
Mazdak Avatar answered Sep 20 '22 06:09

Mazdak