How to validate Indian driving license number using Regular Expression | Python program to validate Indian driving license number using regular expression ; Function to validate Indian driving license number . ; Regex to check valid Indian driving license 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 isValidLicenseNo ( str ) : NEW_LINE INDENT regex = ( " ^ ( ( [ A - Z ] {2 } [ 0-9 ] { 2 } ) " + " ( ▁ ) | ( [ A - Z ] {2 } - [0-9 ] " + " { 2 } ) ) ( (19 ▁ 20 ) [ 0-9 ] " + " [ 0-9 ] ) [0-9 ] { 7 } $ " ) NEW_LINE 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 = " HR - 0619850034761" NEW_LINE print ( isValidLicenseNo ( str1 ) ) NEW_LINE str2 = " UP14 ▁ 20160034761" NEW_LINE print ( isValidLicenseNo ( str2 ) ) NEW_LINE str3 = "12HR - 37200602347" NEW_LINE print ( isValidLicenseNo ( str3 ) ) NEW_LINE str4 = " MH27 ▁ 30123476102" NEW_LINE print ( isValidLicenseNo ( str4 ) ) NEW_LINE str5 = " GJ - 2420180" NEW_LINE print ( isValidLicenseNo ( str5 ) ) NEW_LINE