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 ?
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.
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.
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With