How to validate Indian driving license number using Regular Expression

Given string str, the task is to check whether the given string is a valid Indian driving license number or not by using Regular Expression.
The valid Indian driving license number must satisfy the following conditions: 

  1. It should be 16 characters long (including space or hyphen (-)).
  2. The driving license number can be entered in any of the following formats: 
HR-0619850034761
 OR 
HR06 19850034761
  1. The first two characters should be upper case alphabets that represent the state code.
  2. The next two characters should be digits that represent the RTO code.
  3. The next four characters should be digits that represent the license issued year.
  4. The next seven characters should be any digits from 0-9.

Note: In this article, we will check the license issued year from 1900-2099. It can be customized to change the license issued year.
Examples:

Input: str = “HR-0619850034761”; 
Output: true 
Explanation: 
The given string satisfies all the above mentioned conditions. Therefore, it is not a valid Indian driving license number.
Input: str = “MH27 30120034761”; 
Output: false 
Explanation: 
The given string has the license issued year 3012, that is not a valid year because in this article we validate the year from 1900-2099. Therefore, it is not a valid Indian driving license number.
Input: str = “GJ-2420180”; 
Output: false 
Explanation: 
The given string has 10 characters. Therefore, it is not a valid Indian driving license number. 
 

Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer: 

  • Get the String.
  • Create a regular expression to check valid Indian driving license number as mentioned below: 

regex = “^(([A-Z]{2}[0-9]{2})( )|([A-Z]{2}-[0-9]{2}))((19|20)[0-9][0-9])[0-9]{7}$”; 



  • Where: 
    • ^ represents the starting of the string.
    • ( represents the starting of group 1.
    • ( represents the starting of group 2.
    • [A-Z]{2} represents the first two characters should be upper case alphabets.
    • [0-9]{2} represents the next two characters should be digits.
    • ) represents the ending of the group 2.
    • ( ) represents the white space character.
    • | represents the or.
    • ( represents the starting of group 3.
    • [A-Z]{2} represents the first two characters should be upper case alphabets.
    • represents the hyphen.
    • [0-9]{2} represents the next two characters should be digits.
    • ) represents the ending of the group 3.
    • ) represents the ending of the group 1.
    • ((19|20)[0-9][0-9]) represents the year from 1900-2099.
    • [0-9]{7} represents the next seven characters should be any digits from 0-9.
    • $ represents the ending of the string.
  • Match the given string with the Regular Expression, In Java, this can be done by using Pattern.matcher().
  • Return true if the string matches with the given regular expression, else return false.

Below is the implementation of the above approach:
 

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to validate
// Indian driving license number
// using regular expression
 
import java.util.regex.*;
class GFG {
 
    // Function to validate
    // Indian driving license number
    // using regular expression
    public static boolean
    isValidLicenseNo(String str)
    {
        // Regex to check valid
        // Indian driving license number
        String regex
            = "^(([A-Z]{2}[0-9]{2})"
              + "( )|([A-Z]{2}-[0-9]"
              + "{2}))((19|20)[0-9]"
              + "[0-9])[0-9]{7}$";
 
        // Compile the ReGex
        Pattern p
            = Pattern.compile(regex);
 
        // If the string is empty
        // return false
        if (str == null) {
            return false;
        }
 
        // Find match between given string
        // and regular expression
        // uSing Pattern.matcher()
 
        Matcher m = p.matcher(str);
 
        // Return if the string
        // matched the ReGex
        return m.matches();
    }
 
    // Driver code
    public static void main(String args[])
    {
 
        // Test Case 1:
        String str1
            = "HR-0619850034761";
        System.out.println(
            isValidLicenseNo(str1));
 
        // Test Case 2:
        String str2
            = "UP14 20160034761";
        System.out.println(
            isValidLicenseNo(str2));
 
        // Test Case 3:
        String str3
            = "12HR-37200602347";
        System.out.println(
            isValidLicenseNo(str3));
 
        // Test Case 4:
        String str4
            = "MH27 30123476102";
        System.out.println(
            isValidLicenseNo(str4));
 
        // Test Case 5:
        String str5 = "GJ-2420180";
        System.out.println(
            isValidLicenseNo(str5));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to validate
# Indian driving license number
# using regular expression
 
import re
 
# Function to validate Indian
# driving license number.
def isValidLicenseNo(str):
 
    # Regex to check valid
    # Indian driving license number
    regex = ("^(([A-Z]{2}[0-9]{2})" +
             "( )|([A-Z]{2}-[0-9]" +
             "{2}))((19|20)[0-9]" +
             "[0-9])[0-9]{7}$")
     
    # Compile the ReGex
    p = re.compile(regex)
 
    # If the string is empty
    # return false
    if (str == None):
        return False
 
    # Return if the string
    # matched the ReGex
    if(re.search(p, str)):
        return True
    else:
        return False
 
# Driver code
 
# Test Case 1:
str1 = "HR-0619850034761"
print(isValidLicenseNo(str1))
 
# Test Case 2:
str2 = "UP14 20160034761"
print(isValidLicenseNo(str2))
 
# Test Case 3:
str3 = "12HR-37200602347"
print(isValidLicenseNo(str3))
 
# Test Case 4:
str4 = "MH27 30123476102"
print(isValidLicenseNo(str4))
 
# Test Case 5:
str5 = "GJ-2420180"
print(isValidLicenseNo(str5))
 
# This code is contributed by avanitrachhadiya2155

chevron_right


Output: 

true
true
false
false
false

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

External Technical Content Reviewer at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : avanitrachhadiya2155