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++
#include <iostream>
#include <regex>
using namespace std;
bool isValidLicenseNo(string str)
{
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 (str.empty())
{
return false ;
}
if (regex_match(str, pattern))
{
return true ;
}
else {
return false ;
}
}
int main()
{
string str1 = "HR-0619850034761" ;
cout << isValidLicenseNo(str1) << endl;
string str2 = "UP14 20160034761" ;
cout << isValidLicenseNo(str2) << endl;
string str3 = "12HR-37200602347" ;
cout << isValidLicenseNo(str3) << endl;
string str4 = "MH27 30123476102" ;
cout << isValidLicenseNo(str4) << endl;
string str5 = "GJ-2420180" ;
cout << isValidLicenseNo(str5) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValidLicenseNo(String str)
{
String regex = "^(([A-Z]{2}[0-9]{2})"
+ "( )|([A-Z]{2}-[0-9]"
+ "{2}))((19|20)[0-9]"
+ "[0-9])[0-9]{7}$" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "HR-0619850034761" ;
System.out.println(isValidLicenseNo(str1));
String str2 = "UP14 20160034761" ;
System.out.println(isValidLicenseNo(str2));
String str3 = "12HR-37200602347" ;
System.out.println(isValidLicenseNo(str3));
String str4 = "MH27 30123476102" ;
System.out.println(isValidLicenseNo(str4));
String str5 = "GJ-2420180" ;
System.out.println(isValidLicenseNo(str5));
}
}
|
Python3
import re
def isValidLicenseNo( str ):
regex = ( "^(([A-Z]{2}[0-9]{2})" +
"( )|([A-Z]{2}-[0-9]" +
"{2}))((19|20)[0-9]" +
"[0-9])[0-9]{7}$" )
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "HR-0619850034761"
print (isValidLicenseNo(str1))
str2 = "UP14 20160034761"
print (isValidLicenseNo(str2))
str3 = "12HR-37200602347"
print (isValidLicenseNo(str3))
str4 = "MH27 30123476102"
print (isValidLicenseNo(str4))
str5 = "GJ-2420180"
print (isValidLicenseNo(str5))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
public static bool isValidLicenseNo( string str)
{
string regex = "^(([A-Z]{2}[0-9]{2})"
+ "( )|([A-Z]{2}-[0-9]"
+ "{2}))((19|20)[0-9]"
+ "[0-9])[0-9]{7}$" ;
Regex p = new Regex(regex);
if (str == null ) {
return false ;
}
Match m = p.Match(str);
return m.Success;
}
public static void Main()
{
string str1 = "HR-0619850034761" ;
Console.WriteLine(isValidLicenseNo(str1));
string str2 = "UP14 20160034761" ;
Console.WriteLine(isValidLicenseNo(str2));
string str3 = "12HR-37200602347" ;
Console.WriteLine(isValidLicenseNo(str3));
string str4 = "MH27 30123476102" ;
Console.WriteLine(isValidLicenseNo(str4));
string str5 = "GJ-2420180" ;
Console.WriteLine(isValidLicenseNo(str5));
}
}
|
Javascript
function isValid_License_Number(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 == null ) {
return "false" ;
}
if (regex.test(license_Number) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "HR-0619850034761" ;
console.log(isValid_License_Number(str1));
let str2 = "UP14 20160034761" ;
console.log(isValid_License_Number(str2));
let str3 = "12HR-37200602347" ;
console.log(isValid_License_Number(str3));
let str4 = "MH27 30123476102" ;
console.log(isValid_License_Number(str4));
let str5 = "GJ-2420180" ;
console.log(isValid_License_Number(str5));
let str6 = "RAH12071998" ;
console.log(isValid_License_Number(str6));
|
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)
Share your thoughts in the comments
Please Login to comment...