How to validate GST ( Goods and Services Tax ) number using Regular Expression | Python3 program to validate GST ( Goods and Services Tax ) number using regular expression ; Function to validate GST ( Goods and Services Tax ) number . ; Regex to check valid GST ( Goods and Services Tax ) number ; Compile the ReGex ; If the string is empty return false ; Return if the string matched the ReGex ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
import re NEW_LINE def isValidMasterCardNo ( str ) : NEW_LINE INDENT regex = " ^ [ 0-9 ] { 2 } [ A - Z ] {5 } [ 0-9 ] { 4 } " + NEW_LINE INDENT " [ A - Z ] {1 } [ 1-9A - Z ] {1 } " + NEW_LINE DEDENT p = re . compile ( regex ) NEW_LINE if ( str == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( re . search ( p , str ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT str1 = "06BZAHM6385P6Z2" NEW_LINE print ( isValidMasterCardNo ( str1 ) ) NEW_LINE str2 = "06BZAF67" NEW_LINE print ( isValidMasterCardNo ( str2 ) ) NEW_LINE str3 = " AZBZAHM6385P6Z2" NEW_LINE print ( isValidMasterCardNo ( str3 ) ) NEW_LINE str4 = "06BZ63AHM85P6Z2" NEW_LINE print ( isValidMasterCardNo ( str4 ) ) NEW_LINE str5 = "06BZAHM6385P6F2" NEW_LINE print ( isValidMasterCardNo ( str5 ) ) NEW_LINE