How to validate PAN Card number using Regular Expression

Given string str of alphanumeric characters, the task is to check whether the string is a valid PAN (Permanent Account Number) Card number or not by using Regular Expression.
The valid PAN Card number must satisfy the following conditions: 

  1. It should be ten characters long.
  2. The first five characters should be any upper case alphabets.
  3. The next four-characters should be any number from 0 to 9.
  4. The last(tenth) character should be any upper case alphabet.
  5. It should not contain any white spaces.

Examples:

Input: str = “BNZAA2318J” 
Output: true 
Explanation: 
The given string satisfies all the above mentioned conditions.

Input: str = “23ZAABN18J” 
Output: false 
Explanation: 
The given string not starts with upper case alphabets, therefore it is not a valid PAN Card number.

Input: str = “BNZAA2318JM” 
Output: false 
Explanation: 
The given string contains eleven characters, therefore it is not a valid PAN Card number.



Input: str = “BNZAA23184” 
Output: true 
Explanation: 
The last(tenth) character of this string is a numeric(0-9) character, therefore it is not a valid PAN Card number. 

Input: str = “BNZAA 23184” 
Output: true 
Explanation: 
The given string contains white spaces, therefore it is not a valid PAN Card number. 

Approach: This problem can be solved by using Regular Expression. 

  • Get the string.
  • Create a regular expression to validate the PAN Card number as mentioned below:
regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}";



  • Where: 
    • [A-Z]{5} represents the first five upper case alphabets which can be A to Z.
    • [0-9]{4} represents the four numbers which can be 0-9.
    • [A-Z]{1} represents the one upper case alphabet which can be A to Z.
  • Match the given string with the regex, in Java, this can be done using Pattern.matcher().
  • Return true if the string matches with the given regex, 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 the
// PAN Card number using Regular Expression
 
import java.util.regex.*;
 
class GFG {
 
    // Function to validate the PAN Card number.
    public static boolean isValidPanCardNo(
        String panCardNo)
    {
 
        // Regex to check valid PAN Card number.
        String regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}";
 
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
 
        // If the PAN Card number
        // is empty return false
        if (panCardNo == null) {
            return false;
        }
 
        // Pattern class contains matcher() method
        // to find matching between given
        // PAN Card number using regular expression.
        Matcher m = p.matcher(panCardNo);
 
        // Return if the PAN Card number
        // matched the ReGex
        return m.matches();
    }
 
    // Driver Code.
    public static void main(String args[])
    {
 
        // Test Case 1:
        String str1 = "BNZAA2318J";
        System.out.println(isValidPanCardNo(str1));
 
        // Test Case 2:
        String str2 = "23ZAABN18J";
        System.out.println(isValidPanCardNo(str2));
 
        // Test Case 3:
        String str3 = "BNZAA2318JM";
        System.out.println(isValidPanCardNo(str3));
 
        // Test Case 4:
        String str4 = "BNZAA23184";
        System.out.println(isValidPanCardNo(str4));
 
        // Test Case 5:
        String str5 = "BNZAA 23184";
        System.out.println(isValidPanCardNo(str5));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to validate the
# PAN Card number using Regular
# Expression
import re
 
# Function to validate the
# PAN Card number.
def isValidPanCardNo(panCardNo):
 
    # Regex to check valid
    # PAN Card number
    regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}"
 
    # Compile the ReGex
    p = re.compile(regex)
 
    # If the PAN Card number
    # is empty return false
    if(panCardNo == None):
        return False
 
    # Return if the PAN Card number
    # matched the ReGex
    if(re.search(p, panCardNo) and
       len(panCardNo) == 10):
        return True
    else:
        return False
 
# Driver Code.
 
# Test Case 1:
str1 = "BNZAA2318J"
print(isValidPanCardNo(str1))
 
# Test Case 2:
str2 = "23ZAABN18J"
print(isValidPanCardNo(str2))
 
# Test Case 3:
str3 = "BNZAA2318JM"
print(isValidPanCardNo(str3))
 
# Test Case 4:
str4 = "BNZAA23184"
print(isValidPanCardNo(str4))
 
# Test Case 5:
str5 = "BNZAA 23184"
print(isValidPanCardNo(str5))
 
# This code is contributed by avanitrachhadiya2155

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

External Technical Content Reviewer at GeeksforGeeks

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 : avanitrachhadiya2155