Quantcast
Channel: Code – no dogma blog
Viewing all articles
Browse latest Browse all 132

SSN checking and formatting

$
0
0

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.


using System.Text.RegularExpressions;

namespace NoDogmaBlog
{
  public class SSNFormatter
  {
    private const string IncomingFormat = @"^(d{3})(d{2})(d{4})$";
    private const string OutgoingFormat = "$1-$2-$3";
    readonly Regex regexNotCompiled = new Regex(IncomingFormat);
    readonly Regex regexCompiled = new Regex(IncomingFormat, RegexOptions.Compiled);

    #region Static
    public static string StaticStringRepleacement(string ssnInput)
    {
      var result = Regex.Replace(ssnInput, IncomingFormat, OutgoingFormat);
      return result;
    }

    public static string StaticMatchEvaluatorReplacement(string ssnInput)
    {
      var result = Regex.Replace(ssnInput, IncomingFormat, m => m.Groups[1] +
        "-" + m.Groups[2] + "-" + m.Groups[3]);
      return result;
    }
    #endregion

    #region NotCompiled
    public string InstanceNotCompiledStringReplacement(string ssnInput)
    {
      var result = regexNotCompiled.Replace(ssnInput, OutgoingFormat);
      return result;
    }

    public string InstanceNotCompiledMatchEvaluatorReplaement(string ssnInput)
    {
      var result = regexNotCompiled.Replace(ssnInput, m => m.Groups[1] +
        "-" + m.Groups[2] + "-" + m.Groups[3]);
      return result;
    }
    #endregion

    #region Compiled
    public string InstanceCompiledStringReplacement(string ssnInput)
    {
      var result = regexCompiled.Replace(ssnInput, OutgoingFormat);
      return result;
    }

    public string InstanceCompiledMatchEvaluatorReplaement(string ssnInput)
    {
      var result = regexCompiled.Replace(ssnInput, m => m.Groups[1] + "-"
        + m.Groups[2] + "-" + m.Groups[3]);
      return result;
    }
    #endregion
  }
}

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

Results

MethodTime
StaticStringRepleacement00:00:16.0028520
StaticMatchEvaluatorReplacement00:00:17.5301894
InstanceNotCompiledStringReplacement00:00:11.6908033
InstanceNotCompiledMatchEvaluatorReplaement00:00:13.8301780
InstanceCompiledStringReplacement00:00:09.1909727
InstanceCompiledMatchEvaluatorReplaement00: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.


Viewing all articles
Browse latest Browse all 132

Trending Articles