private string optimize(string regex)
        {
            PatternOptimizer optimizer = new PatternOptimizer();

            optimizer.AutoAddLineEndMarks = true;
            return(optimizer.Optimize(regex));

            string opt = regex;

            opt = Regex.Replace(opt, @"(?<before>.)\\^", "${before}");
            opt = Regex.Replace(opt, @"\\$(?<after>.)", "${after}");
            opt = Regex.Replace(opt, @"\$\^", "");
            opt = Regex.Replace(opt, @"\\?\\$", "?");
            //'opt = Regex.Replace(opt, "(\\s)+", Regex.Unescape("\\s+"))
            opt = Regex.Replace(opt, @"\++", Regex.Unescape(@"\+"));
            //'opt = Regex.Replace(opt, "\\+\\*", "+")
            opt = opt.Replace("?+", "?");
            opt = Regex.Replace(opt, doesNotStartWithEscapeChar("${"), "{");
            opt = Regex.Replace(opt, doesNotStartWithEscapeChar("$["), "[");
            opt = Regex.Replace(opt, doesNotStartWithEscapeChar("^?"), "^");

            if (!opt.EndsWith("$"))
            {
                opt += "$";
            }
            if (opt.StartsWith("^["))
            {
                opt = "^" + opt;
            }

            opt = dontAllowIntheMiddle("^", opt);
            opt = dontAllowIntheMiddle("$", opt);

            if (regex.StartsWith("^") && !opt.StartsWith("^"))
            {
                opt = "^" + opt;
            }
            return(opt);
        }
        private string optimize(string regex)
        {
            PatternOptimizer optimizer = new PatternOptimizer();
            optimizer.AutoAddLineEndMarks = true;
            return optimizer.Optimize(regex);

            string opt = regex;
            opt = Regex.Replace(opt, @"(?<before>.)\\^", "${before}");
            opt = Regex.Replace(opt, @"\\$(?<after>.)", "${after}");
            opt = Regex.Replace(opt, @"\$\^", "");
            opt = Regex.Replace(opt, @"\\?\\$", "?");
            //'opt = Regex.Replace(opt, "(\\s)+", Regex.Unescape("\\s+"))
            opt = Regex.Replace(opt, @"\++", Regex.Unescape(@"\+"));
            //'opt = Regex.Replace(opt, "\\+\\*", "+")
            opt = opt.Replace("?+", "?");
            opt = Regex.Replace(opt, doesNotStartWithEscapeChar("${"), "{");
            opt = Regex.Replace(opt, doesNotStartWithEscapeChar("$["), "[");
            opt = Regex.Replace(opt, doesNotStartWithEscapeChar("^?"), "^");

            if (!opt.EndsWith("$"))
            {
                opt += "$";
            }
            if (opt.StartsWith("^["))
            {
                opt = "^" + opt;
            }

            opt = dontAllowIntheMiddle("^", opt);
            opt = dontAllowIntheMiddle("$", opt);

            if (regex.StartsWith("^") && !opt.StartsWith("^"))
            {
                opt = "^" + opt;

            }
            return opt;
        }
    public void NoPattern()
    {
        string output = opt.Optimize("");

        Assert.AreEqual("", output);
    }