public MatchResult Evaluate(string input, string pattern)
        {
            if (input == null || pattern == null)
            {
                return NonMatchResult(input);
            }

            var regexPattern = BuildRegexPattern(pattern);

            var match = Regex.Match(input, regexPattern, RegexOptions.IgnoreCase);

            if (!match.Success)
            {
                return NonMatchResult(input);
            }

            var matchResult = new MatchResult();
            for (var groupIndex = 1; groupIndex < match.Groups.Count; groupIndex++)
            {
                var group = match.Groups[groupIndex];
                if (group.Value.Length > 0)
                {
                    matchResult.StringParts.Add(new StringPart(group.Value, groupIndex % 2 == 0));
                }
            }

            matchResult.Matched = true;
            matchResult.Score = 1;

            return matchResult;
        }
예제 #2
0
        public MatchResult Evaluate(string input, string pattern)
        {
            if (input == null || pattern == null)
            {
                return NonMatchResult(input);
            }

            var match = Regex.Match(input, "(.*)(" + Regex.Escape(pattern) + ")(.*)", RegexOptions.IgnoreCase);

            if (!match.Success)
            {
                return NonMatchResult(input);
            }

            var matchResult = new MatchResult();
            if (match.Groups[1].Length > 0)
            {
                matchResult.StringParts.Add(new StringPart(match.Groups[1].Value));
            }

            if (match.Groups[2].Length > 0)
            {
                matchResult.StringParts.Add(new StringPart(match.Groups[2].Value, true));
            }

            if (match.Groups[3].Length > 0)
            {
                matchResult.StringParts.Add(new StringPart(match.Groups[3].Value));
            }

            matchResult.Matched = true;
            matchResult.Score = 2;

            return matchResult;
        }
 private static MatchResult NonMatchResult(string input)
 {
     var matchResult = new MatchResult();
     if (input != null)
     {
         matchResult.StringParts.Add(new StringPart(input));
     }
     return matchResult;
 }
        public MatchResult Evaluate(string input, string pattern)
        {
            if (input == null || pattern == null)
            {
                return NonMatchResult(input);
            }

            var regexPattern = BuildRegexPattern(pattern);

            var match = Regex.Match(input, regexPattern);

            if (!match.Success)
            {
                return NonMatchResult(input);
            }

            var matchResult = new MatchResult();

            var beforeMatch = input.Substring(0, match.Index);
            matchResult.StringParts.Add(new StringPart(beforeMatch));

            for (var groupIndex = 1; groupIndex < match.Groups.Count; groupIndex++)
            {
                var group = match.Groups[groupIndex];
                if (group.Value.Length > 0)
                {
                    matchResult.StringParts.Add(new StringPart(group.Value, groupIndex%2 == 0));
                }
            }

            var afterMatch = input.Substring(match.Index + match.Length);
            matchResult.StringParts.Add(new StringPart(afterMatch));

            matchResult.Matched = true;
            matchResult.Score = 2;

            return matchResult;
        }