Exemplo n.º 1
0
        private static Exception _TryParse(string text, out GlobTemplate result)
        {
            MatchCollection matches       = EXPR_FORMAT.Matches(text);
            int             previousIndex = 0;
            StringBuilder   regexBuilder  = new StringBuilder();
            StringBuilder   globBuilder   = new StringBuilder();
            List <string>   variables     = new List <string>();

            foreach (Match match in matches)
            {
                var literal = text.Substring(previousIndex, match.Index - previousIndex);

                regexBuilder.Append(Regex.Escape(literal));
                globBuilder.Append(literal);
                string expText = match.Groups["Expression"].Value;
                regexBuilder.AppendFormat("(?<{0}>.*?)", expText);
                globBuilder.Append("*");
                previousIndex = match.Index + match.Length;
                variables.Add(expText);
            }

            var literal2 = text.Substring(previousIndex, text.Length - previousIndex);

            regexBuilder.Append(Regex.Escape(literal2));
            globBuilder.Append(literal2);
            regexBuilder.Append("$");

            string regex = regexBuilder.ToString();

            // remove ./ except if the string starts with it
            regex = Regex.Replace(regex, @"(?<!^)\\\./", "");
            regex = "^.*" + regex.Replace(@"\./", "/");

            result = new GlobTemplate(variables, globBuilder.ToString(), regex);
            return(null);
        }
Exemplo n.º 2
0
 public static bool TryParse(string text, out GlobTemplate result)
 {
     return(_TryParse(text, out result) == null);
 }