Skip to content
Related Articles
Open in App
Not now

Related Articles

Python Regex – Program to accept string starting with vowel

Improve Article
Save Article
  • Last Updated : 12 Jan, 2023
Improve Article
Save Article

Prerequisite: Regular expression in Python
Given a string, write a Python program to check whether the given string is starting with Vowel or Not.
Examples: 

  
Input: animal
Output: Accepted

Input: zebra
Output: Not Accepted

In this program, we are using search() method of re module.
re.search() : This method either returns None (if the pattern doesn’t match), or re.MatchObject that contains information about the matching part of the string. This method stops after the first match, so this is best suited for testing a regular expression more than extracting data. 
Let’s see the Python program for this : 
 

Python3




# Python program to accept string starting with a vowel
 
# import re module
 
# re module provides support
# for regular expressions
import re
 
# Make a regular expression
# to accept string starting with vowel
regex = '^[aeiouAEIOU][A-Za-z0-9_]*'
     
# Define a function for
# accepting string start with vowel
def check(string):
 
     # pass the regular expression
     # and the string in search() method
    if(re.search(regex, string)):
        print("Valid")
         
    else:
        print("Invalid")
     
 
# Driver Code
if __name__ == '__main__' :
     
    # Enter the string
    string = "ankit"
     
    # calling run function
    check(string)
 
    string = "geeks"
    check(string)
 
    string = "sandeep"
    check(string)

Output: 

Valid
Invalid
Invalid

 

Using re.findall:

We are using the re module to work with regular expressions in Python. The re.findall method is used to search for all the occurrences of the regular expression in the given string. If a match is found, it returns a list containing the matches. If no match is found, it returns an empty list.

We have defined a regular expression to match strings that start with a vowel (either uppercase or lowercase). The regular expression ‘^[aeiouAEIOU][A-Za-z0-9_]*’ means:

  • ^ matches the start of a string
  • [aeiouAEIOU] matches any of the characters a, e, i, o, u, A, E, I, O, U
  • [A-Za-z0-9_]* matches any character in the range A-Z, a-z, 0-9, or _, zero or more times

We pass the regular expression and the string to the re.findall method. If a match is found, the match variable will contain a list with the matching string. If no match is found, the match variable will be an empty list. We can then check if the match variable is non-empty to determine if the string starts with a vowel or not.

Python3




# Python program to accept string starting with a vowel
import re
 
def check(string):
    # Make a regular expression to accept string starting with vowel
    regex = '^[aeiouAEIOU][A-Za-z0-9_]*'
     
    # Use the findall method to search for the regular expression in the string
    match = re.findall(regex, string)
     
    if match:
        print("Valid")
    else:
        print("Invalid")
 
# Driver Code
if __name__ == '__main__' :
      
    # Enter the string
    string = "ankit"
      
    # calling run function
    check(string)
  
    string = "geeks"
    check(string)
  
    string = "sandeep"
    check(string)
#This code is contributed by Edula Vinay Kumar Reddy

Output

Valid
Invalid
Invalid

Time complexity: O(n) where n is the length of the string
Auxiliary Space: O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!