How to validate PAN Card number using Regular Expression | Python3 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 ; Return if the PAN Card number matched the ReGex ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
import re NEW_LINE def isValidPanCardNo ( panCardNo ) : NEW_LINE INDENT regex = " [ A - Z ] {5 } [ 0-9 ] { 4 } [ A - Z ] {1 } " NEW_LINE p = re . compile ( regex ) NEW_LINE if ( panCardNo == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( re . search ( p , panCardNo ) and len ( panCardNo ) == 10 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT str1 = " BNZAA2318J " NEW_LINE print ( isValidPanCardNo ( str1 ) ) NEW_LINE str2 = "23ZAABN18J " NEW_LINE print ( isValidPanCardNo ( str2 ) ) NEW_LINE str3 = " BNZAA2318JM " NEW_LINE print ( isValidPanCardNo ( str3 ) ) NEW_LINE str4 = " BNZAA23184" NEW_LINE print ( isValidPanCardNo ( str4 ) ) NEW_LINE str5 = " BNZAA ▁ 23184" NEW_LINE print ( isValidPanCardNo ( str5 ) ) NEW_LINE