Regex
regex
December 1, 20224 min read

Regex for India Driving License validation

A driving license is an official document that authorises a person to operate or drive various types of motor vehicles in public roads. In this article let's understand how we can create a regex for India Driving License and how regex can be matched for India Driving License.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Structure of India Driving License

  • 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.

Regex for checking if India Driving License is valid or not

Regular Expression-

/^(([A-Z]{2}[0-9]{2})( )|([A-Z]{2}-[0-9]{2}))((19|20)[0-9][0-9])[0-9]{7}$/gm

Test string examples for the above regex-

Input StringMatch Output
KA06 208 22 4341does not match
KA06 20812234341matches
HR-ava2a51124741does not match
HR-0219251124741matches

Here is a detailed explanation of the above regex-

/^(([A-Z]{2}[0-9]{2})( )|([A-Z]{2}-[0-9]{2}))((19|20)[0-9][0-9])[0-9]{7}$/gm

^ asserts position at start of a line
1st Capturing Group (([A-Z]{2}[0-9]{2})( )|([A-Z]{2}-[0-9]{2}))
1st Alternative ([A-Z]{2}[0-9]{2})( )
2nd Capturing Group ([A-Z]{2}[0-9]{2})
Match a single character present in the list below [A-Z]
{2} matches the previous token exactly 2 times
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
Match a single character present in the list below [0-9]
{2} matches the previous token exactly 2 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
3rd Capturing Group ( )
  matches the character   with index 3210 (2016 or 408) literally (case sensitive)
2nd Alternative ([A-Z]{2}-[0-9]{2})
4th Capturing Group ([A-Z]{2}-[0-9]{2})
Match a single character present in the list below [A-Z]
{2} matches the previous token exactly 2 times
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
Match a single character present in the list below [0-9]
{2} matches the previous token exactly 2 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
5th Capturing Group ((19|20)[0-9][0-9])
6th Capturing Group (19|20)
1st Alternative 19
19 matches the characters 19 literally (case sensitive)
2nd Alternative 20
20 matches the characters 20 literally (case sensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
Match a single character present in the list below [0-9]
{7} matches the previous token exactly 7 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Hope this article was useful to match India driving license regex pattern.

Share this blog
Tagged in :
regex
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
December 1, 20223 min read
Next Article