VOOZH about

URL: https://www.geeksforgeeks.org/dsa/how-to-validate-indian-driving-license-number-using-regular-expression/

⇱ How to validate Indian driving license number using Regular Expression - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to validate Indian driving license number using Regular Expression

Last Updated : 15 Jul, 2025

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:


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)  

Comment