Beispiel #1
0
        /// <summary>
        /// Validate against NULL or empty string inputs.
        /// Perform the text matching, and return all matches of the subtext in the input text using the Boyer-Moore algorithm, or otherwise "no matches".
        /// </summary>
        /// <param name="text">text to be searched</param>
        /// <param name="subText">pattern to search for</param>
        /// <returns>The character positions of the beginning of each match for the subtext within the text, or "no matches" if no match is found.</returns>
        public static string MatchText(string text, string subText)
        {
            if (String.IsNullOrEmpty(text) || String.IsNullOrEmpty(subText))
            {
                return("null or empty string is not valid input");
            }

            CodeTest ct  = new CodeTest(subText);
            var      res = ct.BoyerMooreMatchAlgo(text);

            StringBuilder sb = new StringBuilder();

            foreach (var c in res)
            {
                sb.Append(c);
                sb.Append(", ");
            }

            string ret = "<no matches>";

            if (sb.Length > 0)
            {
                ret = sb.Remove(sb.Length - 2, 2).ToString();
            }

            return(ret);
        }
Beispiel #2
0
        static void _Main(string[] args)
        {
            string text    = "Polly put the kettle on, polly put the kettle on, polly put the kettle on we'll all have tea";
            string subtext = "Polly";
            string ret     = CodeTest.MatchText(text, subtext);

            Console.WriteLine("**********************************************");
            Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret);

            subtext = "polly";
            ret     = CodeTest.MatchText(text, subtext);
            Console.WriteLine("\n**********************************************");
            Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret);

            subtext = "ll";
            ret     = CodeTest.MatchText(text, subtext);
            Console.WriteLine("\n**********************************************");
            Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret);

            subtext = "Ll";
            ret     = CodeTest.MatchText(text, subtext);
            Console.WriteLine("\n**********************************************");
            Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret);

            subtext = "X";
            ret     = CodeTest.MatchText(text, subtext);
            Console.WriteLine("\n**********************************************");
            Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret);

            subtext = "Polx";
            ret     = CodeTest.MatchText(text, subtext);
            Console.WriteLine("\n**********************************************");
            Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret);

            subtext = "eTtLe ON";
            ret     = CodeTest.MatchText(text, subtext);
            Console.WriteLine("\n**********************************************");
            Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret);

            subtext = null;
            ret     = CodeTest.MatchText(text, subtext);
            Console.WriteLine("\n**********************************************");
            Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret);
        }