/// <summary> /// Validate against NULL or empty string inputs. /// Perform the text matching, and return all matches of the subtext in the input text, or "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"); } FastStringMatch ct = new FastStringMatch(subText); var res = ct.MatchAlgo(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); }
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 = FastStringMatch.MatchText(text, subtext); Console.WriteLine("**********************************************"); Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret); subtext = "polly"; ret = FastStringMatch.MatchText(text, subtext); Console.WriteLine("\n**********************************************"); Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret); subtext = "ll"; ret = FastStringMatch.MatchText(text, subtext); Console.WriteLine("\n**********************************************"); Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret); subtext = "Ll"; ret = FastStringMatch.MatchText(text, subtext); Console.WriteLine("\n**********************************************"); Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret); subtext = "X"; ret = FastStringMatch.MatchText(text, subtext); Console.WriteLine("\n**********************************************"); Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret); subtext = "Polx"; ret = FastStringMatch.MatchText(text, subtext); Console.WriteLine("\n**********************************************"); Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret); subtext = "eTtLe ON"; ret = FastStringMatch.MatchText(text, subtext); Console.WriteLine("\n**********************************************"); Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret); subtext = null; ret = FastStringMatch.MatchText(text, subtext); Console.WriteLine("\n**********************************************"); Console.WriteLine("The input text: {0}\nSubtext: {1}\nOutput: {2}", text, subtext, ret); }