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:
- It should be 16 characters long (including space or hyphen (-)).
- The driving license number can be entered in any of the following formats:
HR-0619850034761 OR HR06 19850034761
- The first two characters should be upper case alphabets that represent the state code.
- The next two characters should be digits that represent the RTO code.
- The next four characters should be digits that represent the license issued year.
- 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
// Java program to validate// Indian driving license number// using regular expressionimport 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 expressionimport 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 the// Indian driving license number// using Regular Expression#include <iostream>#include <regex>using namespace std;// Function to validate the// Indian driving license numberbool 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 Codeint 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 |
true true false false false



.png)