How to validate Indian driving license number using Regular Expression
Last Updated :
23 Dec, 2022
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 in a 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 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
Outputtrue
true
false
false
false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Similar Reads
How to validate Indian Passport number using Regular Expression
Given a string str of alphanumeric characters, the task is to check whether the given string is a valid passport number or not by using Regular Expression. A valid passport number in India must satisfy the following conditions: It should be eight characters long.The first character should be an uppe
5 min read
How to validate CVV number using Regular Expression
Given string str, the task is to check whether it is a valid CVV (Card Verification Value) number or not by using Regular Expression. The valid CVV (Card Verification Value) number must satisfy the following conditions: It should have 3 or 4 digits.It should have a digit between 0-9.It should not ha
5 min read
How to validate pin code of India using Regular Expression
Given a string of positive number ranging from 0 to 9, the task is to check whether the number is valid pin code or not by using a Regular Expression. The valid pin code of India must satisfy the following conditions. It can be only six digits.It should not start with zero.First digit of the pin cod
6 min read
How to validate MasterCard number using Regular Expression
Given string str, the task is to check whether the given string is a valid Master Card number or not by using Regular Expression. The valid Master Card number must satisfy the following conditions. It should be 16 digits long.It should start with either two digits numbers may range from 51 to 55 or
7 min read
How to validate a domain name using Regular Expression
Given string str, the task is to check whether the given string is a valid domain name or not by using Regular Expression.The valid domain name must satisfy the following conditions: The domain name should be a-z or A-Z or 0-9 and hyphen (-).The domain name should be between 1 and 63 characters long
6 min read
How to validate PAN Card number using Regular Expression
Given string str of alphanumeric characters, the task is to check whether the string is a valid PAN (Permanent Account Number) Card number or not by using Regular Expression.The valid PAN Card number must satisfy the following conditions: It should be ten characters long.The first five characters sh
6 min read
How to validate Visa Card number using Regular Expression
Given a string str, the task is to check whether the given string is a valid Visa Card number or not by using Regular Expression. The valid Visa Card number must satisfy the following conditions: It should be 13 or 16 digits long, new cards have 16 digits and old cards have 13 digits.It should start
6 min read
How to Validate MICR Code using Regular Expression?
MICR stands for Magnetic Ink Character Recognition. This technology provides transaction security, ensuring the correctness of bank cheques. MICR code makes cheque processing faster and safer. MICR Technology reduces cheque-related fraudulent activities. Structure of a Magnetic Ink Character Recogni
5 min read
How to validate ISIN using Regular Expressions
ISIN stands for International Securities Identification Number. Given string str, the task is to check whether the given string is a valid ISIN(International Securities Identification Number) or not by using Regular Expression. The valid ISIN(International Securities Identification Number) must sati
6 min read
How to validate IFSC Code using Regular Expression
Given string str, the task is to check whether the given string is a valid IFSC (Indian Financial System) Code or not by using Regular Expression. The valid IFSC (Indian Financial System) Code must satisfy the following conditions: It should be 11 characters long.The first four characters should be
8 min read