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 a valid passport number or not by using Regular Expression.
A valid passport number in India must satisfy the following conditions:
- 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.
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.
- Get the String.
- 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.
- 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:
C++
#include <iostream>
#include <regex>
using namespace std;
bool isValidPassportNo(string str)
{
const regex pattern( "^[A-PR-WY-Z][1-9]"
"\\d\\s?\\d{4}[1-9]$" );
if (str.empty()) {
return false ;
}
if (regex_match(str, pattern)) {
return true ;
}
else {
return false ;
}
}
int main()
{
string str1 = "A21 90457" ;
cout << isValidPassportNo(str1) << endl;
string str2 = "A0296457" ;
cout << isValidPassportNo(str2) << endl;
string str3 = "Q2096453" ;
cout << isValidPassportNo(str3) << endl;
string str4 = "12096457" ;
cout << isValidPassportNo(str4) << endl;
string str5 = "A209645704" ;
cout << isValidPassportNo(str5) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValidPassportNo(String str)
{
String regex = "^[A-PR-WY][1-9]\\d"
+ "\\s?\\d{4}[1-9]$" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "A21 90457" ;
System.out.println(isValidPassportNo(str1));
String str2 = "A0296457" ;
System.out.println(isValidPassportNo(str2));
String str3 = "Q2096453" ;
System.out.println(isValidPassportNo(str3));
String str4 = "12096457" ;
System.out.println(isValidPassportNo(str4));
String str5 = "A209645704" ;
System.out.println(isValidPassportNo(str5));
}
}
|
Python3
import re
def isValidPassportNo(string):
regex = "^[A-PR-WY][1-9]\\d" + \
"\\s?\\d{4}[1-9]$"
p = re. compile (regex)
if (string = = ''):
return False
m = re.match(p, string)
if m is None :
return False
else :
return True
if __name__ = = "__main__" :
str1 = "A21 90457"
print (isValidPassportNo(str1))
str2 = "A0296457"
print (isValidPassportNo(str2))
str3 = "Q2096453"
print (isValidPassportNo(str3))
str4 = "12096457"
print (isValidPassportNo(str4))
str5 = "A209645704"
print (isValidPassportNo(str5))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main( string [] args)
{
string [] str={ "A21 90457" , "A0296457" , "Q2096453" , "12096457" ,
"A209645704" };
foreach ( string s in str) {
Console.WriteLine( isValidPassportNo(s) ? "true" : "false" );
}
Console.ReadKey(); }
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 );
}
}
|
Javascript
function isValidPassportNo(str) {
let regex = new RegExp(/^[A-PR-WY][1-9]\d\s?\d{4}[1-9]$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "A21 90457" ;
console.log(isValidPassportNo(str1));
let str2 = "A0296457" ;
console.log(isValidPassportNo(str2));
let str3 = "Q2096453" ;
console.log(isValidPassportNo(str3));
let str4 = "12096457" ;
console.log(isValidPassportNo(str4));
let str5 = "A209645704" ;
console.log(isValidPassportNo(str5));
|
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)
Last Updated :
04 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...