I have a method in C# that says FormatSSN that takes a SSN in a string format and replaces the dashes. I mean I am expecting the SSN to be in XXX-XX-XXXX format. I want to write a regular expression that makes sure that the SSN is in the format I have mentioned.
Can anyone help me construct a regular expression??
\dis a digit,{X}is repeat the previous elementXtimes.As Dmitry pointed out in comments, adding
^at the beginning and$at the end will cause the regex to only match if the entire string is a SSN. Without those anchors strings likeabc123-45-6789xyzwould also match.