Last active
December 15, 2015 09:09
-
-
Save shiftkey/5236161 to your computer and use it in GitHub Desktop.
Test Harness for a regex that is giving me hell right now
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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