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
Method | Time |
---|---|
StaticStringRepleacement | 00:00:16.0028520 |
StaticMatchEvaluatorReplacement | 00:00:17.5301894 |
InstanceNotCompiledStringReplacement | 00:00:11.6908033 |
InstanceNotCompiledMatchEvaluatorReplaement | 00:00:13.8301780 |
InstanceCompiledStringReplacement | 00:00:09.1909727 |
InstanceCompiledMatchEvaluatorReplaement | 00:00:11.5331829 |