How to validate PAN Card number using Regular Expression


  1. Get the string.
  2. Create a regular expression to validate the PAN Card number

regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}";

Where:

  1. [A-Z]{5} represents the first five upper case alphabets which can be A to Z
  2. [0-9]{4} represents the four numbers which can be 0-9
  3. [A-Z]{1} represents the one upper case alphabet which can be A to Z

<!-- 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));
}
}

Output

true
false
false
false
false