India Passport is an official travel document issued by the Government of India to its citizens for international travel. It is a document that certifies the identity and nationality of the bearer. It facilitates the bearer to travel to foreign countries, and to return to India. It also serves as proof of Indian citizenship. In this article let’s understand how we can create a regex for India Passport and how regex can be matched for India Passport.
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 Passport
- It should be eight characters long.
- The first character should be an uppercase alphabet.
- The next two characters should be a number, but the first character should be any number from 1-9 and the second character should be any number from 0-9.
- It should be zero or one white space character.
- The next four characters should be any number from 0-9.
- The last character should be any number from 1-9.
Regex for checking if India Passport is valid or not
Regular Expression-
/^[A-PR-WYa-pr-wy][1-9]\d\s?\d{4}[1-9]$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
Q20 DSADG | does not match |
D6143421 | matches |
X614123421 | does not match |
B34 23411 | matches |
Here is a detailed explanation of the above regex-
/^[A-PR-WYa-pr-wy][1-9]\d\s?\d{4}[1-9]$/gm
Match a single character present in the list below [A-PR-WYa-pr-wy]
A-P matches a single character in the range between A (index 65) and P (index 80) (case insensitive)
R-W matches a single character in the range between R (index 82) and W (index 87) (case insensitive)
Y matches the character Y with index 8910 (5916 or 1318) literally (case insensitive)
a-p matches a single character in the range between a (index 97) and p (index 112) (case insensitive)
r-w matches a single character in the range between r (index 114) and w (index 119) (case insensitive)
y matches the character y with index 12110 (7916 or 1718) literally (case insensitive)
Match a single character present in the list below [1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case insensitive)
\d matches a digit (equivalent to [0-9])
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
Match a single character present in the list below [1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case insensitive)
$ 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)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
Hope this article was useful to match India passport regex pattern. In conclusion, the India Passport serves as a vital document for international travel, certifying the bearer’s identity and nationality. Understanding and implementing regular expressions (regex) for validating India Passport patterns can greatly aid in ensuring the accuracy and validity of this essential travel document. Regex is a powerful tool with various applications in programming, text processing, and data validation.