How to validate PAN Card number using Regular Expression | Java program to validate the PAN Card number using Regular Expression ; Function to validate the PAN Card number . ; Regex to check valid PAN Card number . ; Compile the ReGex ; If the PAN Card number is empty return false ; Pattern class contains matcher ( ) method to find matching between given PAN Card number using regular expression . ; Return if the PAN Card number matched the ReGex ; Driver Code . ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
import java . util . regex . * ; class GFG { public static boolean isValidPanCardNo ( String panCardNo ) { String regex = " [ A - Z ] { 5 } [ 0-9 ] { 4 } [ A - Z ] { 1 } " ; Pattern p = Pattern . compile ( regex ) ; if ( panCardNo == null ) { return false ; } Matcher m = p . matcher ( panCardNo ) ; return m . matches ( ) ; } public static void main ( String args [ ] ) { String str1 = " BNZAA2318J " ; System . out . println ( isValidPanCardNo ( str1 ) ) ; String str2 = "23ZAABN18J " ; System . out . println ( isValidPanCardNo ( str2 ) ) ; String str3 = " BNZAA2318JM " ; System . out . println ( isValidPanCardNo ( str3 ) ) ; String str4 = " BNZAA23184" ; System . out . println ( isValidPanCardNo ( str4 ) ) ; String str5 = " BNZAA ▁ 23184" ; System . out . println ( isValidPanCardNo ( str5 ) ) ; } }