From ec1296abb4cfd56041aff2a77002985570478678 Mon Sep 17 00:00:00 2001 From: Clinton Date: Mon, 20 Jun 2016 16:38:43 -0500 Subject: [PATCH] another RegexFromSamples example --- src/Scratch/RegexFromSamples/Demo.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Scratch/RegexFromSamples/Demo.cs b/src/Scratch/RegexFromSamples/Demo.cs index b3ec0a1..42383b5 100644 --- a/src/Scratch/RegexFromSamples/Demo.cs +++ b/src/Scratch/RegexFromSamples/Demo.cs @@ -64,10 +64,20 @@ public void Given_Sample_C() GenerateRegex(target, dontMatch, 9); } + [Test] + public void Given_Sample_D() + { + // http://stackoverflow.com/questions/15807365/find-simplest-regular-expression-matching-all-given-strings?rq=1 + var target = new[] { "h_q1_a", "h_q1_b", "h_q1_c", "h_p2_a", "h_p2_b", "h_p2_c" }; + var dontMatch = new string[] { }; + + GenerateRegex(target, dontMatch, 16); + } + private static void GenerateRegex(IEnumerable target, IEnumerable dontMatch, int expectedLength) { string distinctSymbols = new String(target.SelectMany(x => x).Distinct().ToArray()); - string genes = distinctSymbols + "?*()+"; + string genes = distinctSymbols + "?*()[^]+"; Func calcFitness = str => {