How to validate Indian Passport number using Regular Expression | Python3 program to validate passport number of India using regular expression ; Function to validate the pin code of India . ; Regex to check valid pin code of India . ; Compile the ReGex ; If the string is empty return false ; Pattern class contains matcher ( ) method to find matching between given string and regular expression . ; Return True if the string matched the ReGex else False ; Driver code . ; Test Case 1 ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
import re NEW_LINE def isValidPassportNo ( string ) : NEW_LINE INDENT regex = " ^ [ A - PR - WYa - pr - wy ] [1-9 ] \\d " + p = re . compile ( regex ) NEW_LINE if ( string == ' ' ) : NEW_LINE INDENT return False NEW_LINE DEDENT m = re . match ( p , string ) NEW_LINE if m is None : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " A21 ▁ 90457" NEW_LINE print ( isValidPassportNo ( str1 ) ) NEW_LINE str2 = " A0296457" NEW_LINE print ( isValidPassportNo ( str2 ) ) NEW_LINE str3 = " Q2096453" NEW_LINE print ( isValidPassportNo ( str3 ) ) NEW_LINE str4 = "12096457" NEW_LINE print ( isValidPassportNo ( str4 ) ) NEW_LINE str5 = " A209645704" NEW_LINE print ( isValidPassportNo ( str5 ) ) NEW_LINE DEDENT