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 String | Match Output |
---|---|
KA06 208 22 4341 | does not match |
KA06 20812234341 | matches |
HR-ava2a51124741 | does not match |
HR-0219251124741 | matches |
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. In conclusion, understanding and creating regular expressions (regex) for validating India Driving Licenses can greatly enhance data validation and processing tasks. By breaking down the structure of an India Driving License and constructing an appropriate regex pattern, we can efficiently determine the validity of license information. Regular expressions offer a versatile tool to validate and manipulate text, contributing to more robust data handling in various applications.