Skip to content

Instantly share code, notes, and snippets.

@xanathar
Created June 3, 2014 16:52
Show Gist options
  • Save xanathar/7c9c367680cf3a9e3b65 to your computer and use it in GitHub Desktop.
Save xanathar/7c9c367680cf3a9e3b65 to your computer and use it in GitHub Desktop.
Example of regex w/named capture & lazy static initialization
const string REGEX = @"((?<id>\d+)\:)?(?<par1>\d+)\-(?<par2>\d+)\-(?<par3>\d+)(\/(?<par4>\d+)\-(?<par5>\d+))?";
private static Lazy<Regex> g_RegEx = new Lazy<Regex>(() => new Regex(REGEX,
RegexOptions.ExplicitCapture | RegexOptions.Compiled | RegexOptions.CultureInvariant));
...
Match m = g_RegEx.Value.Match(value);
Group gID = m.Groups["id"] as Group;
Group gPar1 = m.Groups["par1"] as Group;
Group gPar2 = m.Groups["par2"] as Group;
Group gPar3 = m.Groups["par3"] as Group;
Group gPar4 = m.Groups["par4"] as Group;
Group gPar5 = m.Groups["par5"] as Group;
int par1 = int.Parse(gPar1.Value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment