Open In App

How to validate Indian driving license number using Regular Expression

Improve
Improve
Like Article
Like
Save
Share
Report

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 in a 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 numbers 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 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:

C++




// C++ program to validate the
// Indian driving license number
// using Regular Expression
#include <iostream>
#include <regex>
using namespace std;
 
// Function to validate the
// Indian driving license number
bool isValidLicenseNo(string str)
{
 
    // Regex to check valid
    // Indian driving license number
    const regex pattern("^(([A-Z]{2}[0-9]{2})( "
                        ")|([A-Z]{2}-[0-9]{2}))"
                        "((19|20)[0-"
                        "9][0-9])[0-9]{7}$");
 
    // If the Indian driving
    // license number is empty return false
    if (str.empty())
    {
        return false;
    }
 
    // Return true if the Indian
    // driving license number
    // matched the ReGex
    if (regex_match(str, pattern))
    {
        return true;
    }
    else {
        return false;
    }
}
 
// Driver Code
int main()
{
    // Test Case 1:
    string str1 = "HR-0619850034761";
    cout << isValidLicenseNo(str1) << endl;
 
    // Test Case 2:
    string str2 = "UP14 20160034761";
    cout << isValidLicenseNo(str2) << endl;
 
    // Test Case 3:
    string str3 = "12HR-37200602347";
    cout << isValidLicenseNo(str3) << endl;
 
    // Test Case 4:
    string str4 = "MH27 30123476102";
    cout << isValidLicenseNo(str4) << endl;
 
    // Test Case 5:
    string str5 = "GJ-2420180";
    cout << isValidLicenseNo(str5) << endl;
 
    return 0;
}
 
// This code is contributed by yuvraj_chandra


Java




// 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));
    }
}


Python3




# 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


C#




// C# program to validate
// Indian driving license number
// using regular expression
using System;
using System.Text.RegularExpressions;
 
class GFG {
 
  // Function to validate
  // Indian driving license number
  // using regular expression
  public static bool 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
    Regex p = new Regex(regex);
 
    // If the string is empty
    // return false
    if (str == null) {
      return false;
    }
 
    // Find match between given string
    // and regular expression
    // uSing Pattern.matcher()
 
    Match m = p.Match(str);
 
    // Return if the string
    // matched the ReGex
    return m.Success;
  }
 
  // Driver code
  public static void Main()
  {
 
    // Test Case 1:
    string str1 = "HR-0619850034761";
    Console.WriteLine(isValidLicenseNo(str1));
 
    // Test Case 2:
    string str2 = "UP14 20160034761";
    Console.WriteLine(isValidLicenseNo(str2));
 
    // Test Case 3:
    string str3 = "12HR-37200602347";
    Console.WriteLine(isValidLicenseNo(str3));
 
    // Test Case 4:
    string str4 = "MH27 30123476102";
    Console.WriteLine(isValidLicenseNo(str4));
 
    // Test Case 5:
    string str5 = "GJ-2420180";
    Console.WriteLine(isValidLicenseNo(str5));
  }
}
 
// This code is contributed by Aman Kumar.


Javascript




// Javascript program to validate
// License Number  using Regular Expression
 
// Function to validate the
// license_Number 
function isValid_License_Number(license_Number)
{
    // Regex to check valid
    // license_Number 
    let regex = new RegExp(/^(([A-Z]{2}[0-9]{2})( )|([A-Z]{2}-[0-9]{2}))((19|20)[0-9][0-9])[0-9]{7}$/);
 
    // if license_Number
    // is empty return false
    if (license_Number == null) {
        return "false";
    }
 
    // Return true if the license_Number
    // matched the ReGex
    if (regex.test(license_Number) == true) {
        return "true";
    }
    else {
        return "false";
    }
}
 
// Driver Code
// Test Case 1:
let str1 = "HR-0619850034761";
console.log(isValid_License_Number(str1));
 
// Test Case 2:
let str2 = "UP14 20160034761";
console.log(isValid_License_Number(str2));
 
// Test Case 3:
let str3 = "12HR-37200602347";
console.log(isValid_License_Number(str3));
 
// Test Case 4:
let str4 = "MH27 30123476102";
console.log(isValid_License_Number(str4));
 
// Test Case 5:
let str5 = "GJ-2420180";
console.log(isValid_License_Number(str5));
 
// Test Case 6:
let str6 = "RAH12071998";
console.log(isValid_License_Number(str6));
 
// This code is contributed by Rahul Chauhan


Output

true
true
false
false
false

Time Complexity: O(N) for each testcase, where N is the length of the given string. 
Auxiliary Space: O(1)  



Last Updated : 23 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads