How to validate SSN ( Social Security Number ) using Regular Expression | Python3 program to validate SSN ( Social Security Number ) using regular expression ; Function to validate SSN ( Social Security Number ) . ; Regex to check valid SSN ( Social Security 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 :
import re NEW_LINE def isValidSSN ( str ) : NEW_LINE INDENT regex = " ^ ( ? ! 666 ▁ 000 ▁ 9\\d { 2 } ) \\d { 3 } - ( ? !00 ) \\d { 2 } - ( ? !0{4 } ) \\d { 4 } $ " 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 = "856-45-6789" NEW_LINE print ( isValidSSN ( str1 ) ) NEW_LINE str2 = "000-45-6789" NEW_LINE print ( isValidSSN ( str2 ) ) NEW_LINE str3 = "856-452-6789" NEW_LINE print ( isValidSSN ( str3 ) ) NEW_LINE str4 = "856-45-0000" NEW_LINE print ( isValidSSN ( str4 ) ) NEW_LINE