Python | Program that matches a word containing ‘a’ in the given string by using regular expression

Python | Program that matches a word containing ‘a’ in the given string by using regular expression

Given a statement(string), the task is to check if that string contains any word which contains ‘a’ in it then print that word otherwise print no any word containing ‘a’ in the inputted string.

Examples:

Input: Graphic Era Deemed to be University
Output: Graphic 
         Era
Input: Geeks for Geeks
Output: no any word containing 'a' in the inputted string

Approach: Firstly we have to make a regular expression (regex) object that matches a word that contains ‘a’ then we have to pass a string in the findall() method. the findall() method returns the list of the matched strings. when we got this list we have to loop through it and print each matched word.
Here we use shorthand character class \w.
\w – represent Any letter, numeric digit, or the underscore character.
the symbol * means zero or more occurrences of the character.

Below is the Python3 code implementation :


# Python program that matches a word 
# containing 'a' in the given string

# import required pakages
import re

# define a function to check if the any word 
# of the string containing 'a' , print it .
def check(string) :

    # The regular expression \w*a\w* will match text
    # that has zero or more letter / numeric digit /
    # underscore character followed by 'a', followed by
    # zero or more letter / numeric digit / underscore character .
    # now we pass this regular expression as an
    # argument in the compile method.
    regex = re.compile("\w*a\w*")

    # The findall() method returns all matching strings
    # of the regex pattern in a list.
    # pass the string in findall()
    # method of regex object . 
    match_object = regex.findall(string)

    # if Length of match_object is not
    # equal to zero then it means
    # it contains matched string
    # otherwise it is an empty list i.e. no any
    # matched string is found
    if len(match_object) != 0 :

        # looping through the list because we have to
        # print every word which contains 'a'.
        for word in match_object :
            print(word)
            
    else :
        print(" no any word containing 'a' in the inputted string ")


# Main Code        
if __name__ == '__main__' :

    # Enter the string 
    string = " Graphic Era Deemed to be University "

    # Calling check function
    check(string)
   

Output:

Graphic
Era

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds