Python regex program to accept string ending with alphanumeric character

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Prerequisite: Regular expression in Python
    Given a string, write a Python program to check whether the given string is ending with only alphanumeric character or Not.
    Examples: 
     

    Input: ankitrai326
    Output: Accept
    
    Input: ankirai@
    Output: Discard

    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. 
    Alphanumeric characters in the POSIX/C locale consist of either 36 case-insensitive symbols (A-Z and 0-9) or 62 case-sensitive characters (A-Z, a-z and 0-9).
    Let’s see the Python program for this : 
     

    Python3

    import re

    regex = '[a-zA-z0-9]$'

    def check(string):

        if(re.search(regex, string)):

            print("Accept")

        else:

            print("Discard")

    if __name__ == '__main__' :

        string = "ankirai@"

        check(string)

        string = "ankitrai326"

        check(string)

        string = "ankit."

        check(string)

        string = "geeksforgeeks"

        check(string)

    Output : 
     

    Discard
    Accept
    Discard
    Accept


    When it is required to check if a string ends with an alphanumeric character or not, the regular expression is used. A method is defined that checks to see an alphanumeric characters, and returns the string as output.

    Example

    Below is a demonstration of the same

    import re
    
    regex_expression = '[a-zA-z0-9]$'
    
    def check_string(my_string):
    
       if(re.search(regex_expression, my_string)):
          print("The string ends with alphanumeric character")
    
       else:
          print("The string doesnot end with alphanumeric character")
    
    
    my_string_1 = "Python@"
    print("The string is :")
    print(my_string_1)
    check_string(my_string_1)
    
    my_string_2 = "Python1245"
    print("\nThe string is :")
    print(my_string_2)
    check_string(my_string_2)

    Output

    The string is :
    Python@
    The string doesn’t end with alphanumeric character
    The string is :
    Python1245
    The string ends with alphanumeric character

    Explanation

    • The required packages are imported.

    • A regular expression string is defined.

    • A method named ‘check_string’ is defined, and it takes the string as a parameter.

    • The ‘search’ method is called and checked to see if a string ends with a specific character.

    • Outside the method, the string is defined and displayed on the console.

    • The method is called by passing this string.

    • The output is displayed on the console.

    Python regex program to accept string ending with alphanumeric character

    Updated on 20-Sep-2021 10:41:24

    • Related Questions & Answers
    • Python Program to accept string starting with vowel
    • Java regex program to verify whether a String contains at least one alphanumeric character.
    • MySQL query to select column values ending with certain character/number?
    • Program to find whether a string is alphanumeric.
    • PHP program to remove non-alphanumeric characters from string
    • Alphanumeric Abbreviations of a String in C Program?
    • Program to find sum of digits that are inside one alphanumeric string in Python
    • How to check if a string is alphanumeric in Python?
    • Check if a character is alphanumeric in Arduino
    • Python program to find Most Frequent Character in a String
    • Python program to find Least Frequent Character in a String
    • Java Program to Convert Character to String
    • Python program to find occurrence to each character in given string
    • Python program to accept the strings which contains all vowels
    • Python program for removing nth character from a string

    How do you write alphanumeric in regex?

    For checking if a string consists only of alphanumerics using module regular expression or regex, we can call the re. match(regex, string) using the regex: "^[a-zA-Z0-9]+$".

    How do I check if a string is alphanumeric regex?

    Considering you want to check for ASCII Alphanumeric characters, Try this: "^[a-zA-Z0-9]*$" . Use this RegEx in String. matches(Regex) , it will return true if the string is alphanumeric, else it will return false.

    What is \b in Python regex?

    Inside a character range, \b represents the backspace character, for compatibility with Python's string literals. Matches the empty string, but only when it is not at the beginning or end of a word.

    How do you take alphanumeric input in Python?

    Python String isalnum() Method The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9). Example of characters that are not alphanumeric: (space)!