How to validate pin code of India using Regular Expression | Python3 program to validate the pin code 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 pin code is empty return false ; Pattern class contains matcher ( ) method to find matching between given pin code and regular expression . ; Return True if the pin code matched the ReGex else False ; Driver code ; Test case 1 ; Test case 2 : ; Test case 3 : ; Test case 4 :
import re NEW_LINE def isValidPinCode ( pinCode ) : NEW_LINE INDENT regex = " ^ [ 1-9 ] { 1 } [ 0-9 ] { 2 } \\s { 0,1 } [ 0-9 ] { 3 } $ " ; NEW_LINE p = re . compile ( regex ) ; NEW_LINE if ( pinCode == ' ' ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT m = re . match ( p , pinCode ) ; 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 num1 = "132103" ; NEW_LINE print ( num1 , " : ▁ " , isValidPinCode ( num1 ) ) ; NEW_LINE num2 = "201 ▁ 305" ; NEW_LINE print ( num2 , " : ▁ " , isValidPinCode ( num2 ) ) ; NEW_LINE num3 = "014205" ; NEW_LINE print ( num3 , " : ▁ " , isValidPinCode ( num3 ) ) ; NEW_LINE num4 = "1473598" ; NEW_LINE print ( num4 , " : ▁ " , isValidPinCode ( num4 ) ) ; NEW_LINE DEDENT