# How to validate Indian Passport number using Regular Expression

Last Updated : 04 Sep, 2023

Given a string str of alphanumeric characters, the task is to check whether the given string is a valid passport number or not by using Regular Expression

A valid passport number in India must satisfy the following conditions:

1. It should be eight characters long.
2. The first character should be an uppercase alphabet.
3. 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.
4. It should be zero or one white space character.
5. The next four characters should be any number from 0-9.
6. The last character should be any number from 1-9.

Illustration:

Input: str = "A2096457";
Output: true
Explanation: The given string satisfies all the above mentioned conditions. Therefore it is a valid passport number of India.
Input: str = "12096457";
Output: false
Explanation: The given string doesn't starts with an upper case alphabet. Therefore it is not a valid passport number of India.
Input: str = "A209645704";
Output: false
Explanation: The given string contains 10 characters. Therefore it is not a valid passport number of India.

Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer.

1. Get the String.
2. Create a regular expression to check valid passport number of India as mentioned below:
regex = "^[A-PR-WY-Z][1-9]\\d\\s?\\d{4}[1-9]\$";
Where:
^ represents the starting of the string.
[A-PR-WY-Z] represents the string that should start with A-Z excluding Q and X.
[1-9] represents the second character should be any number from 1-9.
\\d represents the third character should be any number from 0-9.
\\s? represents the string should be zero or one white space character.
\\d{4} represents the next four characters should be any number from 0-9.
[1-9] represents the last character should be any number from 1-9.
\$ represents the ending of the string.
1. Match the given string with the Regular Expression. In Java, this can be done by using Pattern.matcher().
2. Return true if the string matches with the given regular expression, else return false.

Below is the implementation of the above approach:

## C++

 // C++ program to validate the // passport number // using Regular Expression #include #include using namespace std;   // Function to validate the passport number bool isValidPassportNo(string str) {     // Regex to check valid passport number     const regex pattern("^[A-PR-WY-Z][1-9]"                         "\\d\\s?\\d{4}[1-9]\$");       // If the passport number     // is empty return false     if (str.empty()) {         return false;     }       // Return true if the passport number     // matched the ReGex     if (regex_match(str, pattern)) {         return true;     }     else {         return false;     } }   // Driver Code int main() {     // Test Case 1:     string str1 = "A21 90457";     cout << isValidPassportNo(str1) << endl;       // Test Case 2:     string str2 = "A0296457";     cout << isValidPassportNo(str2) << endl;       // Test Case 3:     string str3 = "Q2096453";     cout << isValidPassportNo(str3) << endl;       // Test Case 4:     string str4 = "12096457";     cout << isValidPassportNo(str4) << endl;       // Test Case 5:     string str5 = "A209645704";     cout << isValidPassportNo(str5) << endl;       return 0; }   // This code is contributed by yuvraj_chandra

## Java

 // Java program to validate // passport number of India // using regular expression   import java.util.regex.*;   class GFG {       // Function to validate     // passport number of India     // using regular expression     public static boolean isValidPassportNo(String str)     {         // Regex to check valid.         // passport number of India         String regex = "^[A-PR-WY][1-9]\\d"                        + "\\s?\\d{4}[1-9]\$";           // Compile the ReGex         Pattern p = Pattern.compile(regex);           // If the string is empty         // return false         if (str == null) {             return false;         }           // Find match between given string         // and regular expression         // using Pattern.matcher()         Matcher m = p.matcher(str);           // Return if the string         // matched the ReGex         return m.matches();     }       // Driver code     public static void main(String args[])     {           // Test Case 1:         String str1 = "A21 90457";         System.out.println(isValidPassportNo(str1));           // Test Case 2:         String str2 = "A0296457";         System.out.println(isValidPassportNo(str2));           // Test Case 3:         String str3 = "Q2096453";         System.out.println(isValidPassportNo(str3));           // Test Case 4:         String str4 = "12096457";         System.out.println(isValidPassportNo(str4));           // Test Case 5:         String str5 = "A209645704";         System.out.println(isValidPassportNo(str5));     } }

## Python3

 # Python3 program to validate passport # number of India using regular expression import re   # Function to validate the pin code # of India.     def isValidPassportNo(string):       # Regex to check valid pin code     # of India.     regex = "^[A-PR-WY][1-9]\\d" +\             "\\s?\\d{4}[1-9]\$"       # Compile the ReGex     p = re.compile(regex)       # If the string is empty     # return false     if (string == ''):         return False       # Pattern class contains matcher()     # method to find matching between     # given string and regular expression.     m = re.match(p, string)       # Return True if the string     # matched the ReGex else False     if m is None:         return False     else:         return True     # Driver code. if __name__ == "__main__":       # Test Case 1     str1 = "A21 90457"     print(isValidPassportNo(str1))       # Test Case 2:     str2 = "A0296457"     print(isValidPassportNo(str2))       # Test Case 3:     str3 = "Q2096453"     print(isValidPassportNo(str3))       # Test Case 4:     str4 = "12096457"     print(isValidPassportNo(str4))       # Test Case 5:     str5 = "A209645704"     print(isValidPassportNo(str5))   # This code is contributed by AnkitRai01

## C#

 // C# program to validate // passport number of India // using regular expression using System; using System.Text.RegularExpressions; class GFG {     // Main Method   static void Main(string[] args)   {       // Input strings to Match     // Valid Passport     string[] str={"A21 90457","A0296457","Q2096453","12096457",                   "A209645704"};     foreach(string s in str) {       Console.WriteLine( isValidPassportNo(s) ? "true" : "false");     }     Console.ReadKey(); }     // method containing the regex   public static bool isValidPassportNo(string str)   {     string strRegex = @"^[A-PR-WY][1-9]\d\s?\d{4}[1-9]\$";     Regex re = new Regex(strRegex);     if (re.IsMatch(str))       return (true);     else       return (false);   } }   // This code is contributed by Rahul Chauhan

## Javascript

 // Javascript program to // validate passport // number of India using regular expression   // Function to validate the // Passport Number function isValidPassportNo(str) {     // Regex to check valid     // Passport Number     let regex = new RegExp(/^[A-PR-WY][1-9]\d\s?\d{4}[1-9]\$/);       // if str     // is empty return false     if (str == null) {         return "false";     }       // Return true if the str     // matched the ReGex     if (regex.test(str) == true) {         return "true";     }     else {         return "false";     } }   // Driver Code // Test Case 1: let str1 = "A21 90457"; console.log(isValidPassportNo(str1));   // Test Case 2: let str2 = "A0296457"; console.log(isValidPassportNo(str2));   // Test Case 3: let str3 = "Q2096453"; console.log(isValidPassportNo(str3));   // Test Case 4: let str4 = "12096457"; console.log(isValidPassportNo(str4));   // Test Case 5: let str5 = "A209645704"; console.log(isValidPassportNo(str5));     // This code is contributed by Rahul Chauhan

Output

1
0
0
0
0

Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)