How to validate Indian Passport number using Regular Expression

Given a string str of alphanumeric characters, the task is to check whether the given string is valid passport number or not by using Regular Expression.
The valid passport number of India must satisfy the following conditions:

  1. It should be eight characters long.
  2. The first character should be an upper case 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.

Examples:

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-WYa-pr-wy][1-9]\\d\\s?\\d{4}[1-9]$”;



    Where:

    • ^ represents the starting of the string.
    • [A-PR-WYa-pr-wy] represents the string should be starts with A-Z excluding Q, X, and Z.
    • [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.
  3. Match the given string with the Regular Expression. In Java, this can be done by using Pattern.matcher().
  4. Return true if the string matches with the given regular expression, else return false.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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-WYa-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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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-WYa-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

chevron_right


Output:

true
false
false
false
false

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01