How to find words ending with ing

18,944

Solution 1

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']

Solution 2

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.

Solution 3

Try this. It'll work!

import re
expression = input("please enter an expression: ")
pattern = "\w+ing"
result = re.findall(pattern, expression)
print(result)
Share:
18,944

Related videos on Youtube

Mozein
Author by

Mozein

New programmer. Joined for learning purposes

Updated on July 24, 2022

Comments

  • Mozein
    Mozein almost 2 years

    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 ?