C# – Regular Expressions

by Jeff Fillegar on September 14, 2009

class TstRegExpr
{
    static void Main()
    {
        string[] sentences =
        {
            "cow over the moon",
            "Betsy the Cow",
            "cowering in the corner",
            "no match here"
        };

        string sPattern = "cow";

        foreach (string s in sentences)
        {
            System.Console.Write("{0,24}", s);

            if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
            {
                System.Console.WriteLine(" (match for '{0}' found)", sPattern);
            }
            else
            {
                System.Console.WriteLine();
            }
        }
    }
}

Output

 
       cow over the moon  (match for 'cow' found)
           Betsy the Cow  (match for 'cow' found)
  cowering in the corner  (match for 'cow' found)
           no match here

Leave a Comment

Previous post:

Next post: