예제 #1
0
        /// <summary>
        /// Returns the starting index of the last CharSet match in the string.
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public int GetLastCharSetMatchIndex(string text)
        {
            if (Type != PatternType.CharSet)
            {
                return(-1);
            }
            int  last        = -1;
            int  increment   = 1;
            bool lastMatched = false;

            for (int i = 0; i < text.Length; i += increment)
            {
                increment = 1;
                bool match = false;
                // Check all possible contiguous numbers and use the largest match.
                if (Char.IsNumber(text[i]))
                {
                    string[] conNumbers = StringFunction.GetAdjacentNumbersLeftInclusive(text.Substring(i));
                    foreach (string num in conNumbers)
                    {
                        if (charSet.Contains(num))
                        {
                            match     = true;
                            increment = num.Length;
                        }
                    }
                }
                else if (charSet.Contains(text[i].ToString()))
                {
                    match = true;
                }
                if (match && !lastMatched)
                {
                    last = i;
                }
                if (match)
                {
                    lastMatched = true;
                }
                else
                {
                    lastMatched = false;
                }
            }
            return(last);
        }
예제 #2
0
 /// <summary>
 /// Checks if the string array matches the pattern.
 /// </summary>
 /// <param name="text"></param>
 /// <returns></returns>
 public bool Match(string[] text)
 {
     if (Type == PatternType.Literal)
     {
         return(literal.SequenceEqual(StringFunction.ArrayToString(text)));
     }
     else
     {
         foreach (string s in text)
         {
             if (!charSet.Contains(s))
             {
                 return(false);
             }
         }
         return(true);
     }
 }
예제 #3
0
        private List <string> GetCharacters(string s)
        {
            List <string> result = new List <string>();
            string        text   = s;

            if (text.Contains('-'))
            {
                result.AddRange(StringFunction.ExtractRanges(ref text));
            }
            foreach (char letter in text)
            {
                string l = letter.ToString();
                if (!result.Contains(l))
                {
                    result.Add(l);
                }
            }
            return(result);
        }