SSN checking and formatting

Here’s something I worked on a few days ago. It shows six ways of verifying that an incoming string has nine numeric characters and then returns a string in the standard social security number format. In this example I perform the SSN checking and formatting using: static and instances of Regex; compiled and non-compiled; string replacements and match evaluators. More details on these can be found here.

 1using System.Text.RegularExpressions;
 2
 3namespace NoDogmaBlog
 4{
 5  public class SSNFormatter
 6  {
 7    private const string IncomingFormat = @"^(d{3})(d{2})(d{4})$";
 8    private const string OutgoingFormat = "$1-$2-$3";
 9    readonly Regex regexNotCompiled = new Regex(IncomingFormat);
10    readonly Regex regexCompiled = new Regex(IncomingFormat, RegexOptions.Compiled);
11
12    #region Static
13    public static string StaticStringReplacement(string ssnInput)
14    {
15      var result = Regex.Replace(ssnInput, IncomingFormat, OutgoingFormat);
16      return result;
17    }
18
19    public static string StaticMatchEvaluatorReplacement(string ssnInput)
20    {
21      var result = Regex.Replace(ssnInput, IncomingFormat, m => m.Groups[1] +
22        "-" + m.Groups[2] + "-" + m.Groups[3]);
23      return result;
24    }
25    #endregion
26
27    #region NotCompiled
28    public string InstanceNotCompiledStringReplacement(string ssnInput)
29    {
30      var result = regexNotCompiled.Replace(ssnInput, OutgoingFormat);
31      return result;
32    }
33
34    public string InstanceNotCompiledMatchEvaluatorReplacement(string ssnInput)
35    {
36      var result = regexNotCompiled.Replace(ssnInput, m => m.Groups[1] +
37        "-" + m.Groups[2] + "-" + m.Groups[3]);
38      return result;
39    }
40    #endregion
41
42    #region Compiled
43    public string InstanceCompiledStringReplacement(string ssnInput)
44    {
45      var result = regexCompiled.Replace(ssnInput, OutgoingFormat);
46      return result;
47    }
48
49    public string InstanceCompiledMatchEvaluatorReplacement(string ssnInput)
50    {
51      var result = regexCompiled.Replace(ssnInput, m => m.Groups[1] + "-"
52        + m.Groups[2] + "-" + m.Groups[3]);
53      return result;
54    }
55    #endregion
56  }
57}

I ran these methods on 10,000,000 randomly generated nine digit strings. I consistently observed results similar to those shown below.

Results

MethodTime
StaticStringReplacement00:00:16.0028520
StaticMatchEvaluatorReplacement00:00:17.5301894
InstanceNotCompiledStringReplacement00:00:11.6908033
InstanceNotCompiledMatchEvaluatorReplacement00:00:13.8301780
InstanceCompiledStringReplacement00:00:09.1909727
InstanceCompiledMatchEvaluatorReplacement00:00:11.5331829

Be aware that using a compiled regular expression will suffer from a certain amount of overhead when compiling the expression. This overhead should be taken into consideration when writing shortlived applications.

comments powered by Disqus