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 |
C++
// C++ program to validate the // passport number // using Regular Expression #include <iostream> #include <regex> using namespace std; // Function to validate the passport number bool isValidPassportNo(string str) { // Regex to check valid passport number const regex pattern( "^[A-PR-WYa-pr-wy][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 |
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.