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:
- It should be eight characters long.
- The first character should be an upper case 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.
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.
- Get the String.
- 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.
- Match the given string with the Regular Expression. In Java, this can be done by using Pattern.matcher().
- Return true if the string matches with the given regular expression, else return false.
Below is the implementation of the above approach:
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-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)); } } |
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-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 |
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.
Recommended Posts:
- How to validate Indian driving license number using Regular Expression
- How to validate Visa Card number using Regular Expression
- How to validate PAN Card number using Regular Expression
- How to validate GST (Goods and Services Tax) number using Regular Expression
- How to validate CVV number using Regular Expression
- How to validate MasterCard number using Regular Expression
- How to validate SSN (Social Security Number) using Regular Expression
- How to validate identifier using Regular Expression in Java
- How to validate time in 12-hour format using Regular Expression
- How to validate time in 24-hour format using Regular Expression
- How to validate pin code of India using Regular Expression
- How to validate Hexadecimal Color Code using Regular Expression
- How to validate HTML tag using Regular Expression
- How to validate a domain name using Regular Expression
- How to validate image file extension using Regular Expression
- How to validate IFSC Code using Regular Expression
- How to validate GUID (Globally Unique Identifier) using Regular Expression
- How to validate MAC address using Regular Expression
- How to validate an IP address using Regular Expressions in Java
- How to validate a Password using Regular Expressions in Java
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

