Skip to content

Instantly share code, notes, and snippets.

@shiftkey
Last active December 15, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiftkey/5236161 to your computer and use it in GitHub Desktop.
Save shiftkey/5236161 to your computer and use it in GitHub Desktop.
Test Harness for a regex that is giving me hell right now
using System;
using System.Text.RegularExpressions;
namespace RegexTester
{
class Program
{
private static void Main(string[] args)
{
var allText = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<doc>\r\n<!-- code start foo csharp -->\r\n<foo />\r\n<bar />\r\n<!-- end code foo -->\r\n<baz />\r\n</doc>";
var options = RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.Multiline;
var result = Regex.Matches(allText, @"(?s)code start (?<key>[A-Za-z-]*) (?<language>[A-Za-z]*).*[\n](?<value>.*?)[\n].*end code (?<key>[A-Za-z-]*)", options);
foreach (Match match in result)
{
var key = match.Groups["key"];
var language = match.Groups["language"];
var value = match.Groups["value"];
Console.WriteLine("key: {0}, language: {1}, value: {2}", key, language, value);
}
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment