How to search Strings using Regular Expression in C#

In this article we are discussing about how to C# Strings using Regular Expression.
  • 3955

In this article we are discussing about how to search Strings in C# using Regular Expression. For this we use Regex Class which resides within System.Text.RegularExpressions namespace. Here we will use IsMatch() method to match two strings in order to search them using Regular Expression.

Lets have a look on the following example in C#

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication17

{

    class Program

    {

        static void Main(string[] args)

        {

            string[] str =

        {

            "Help",

            "Chapter 2: Reqiured Help",

            "Helpme",

            "no match found"

        };

 

            string check = "help";

 

            foreach (string ss in str)

            {

                System.Console.Write("{0,24}", ss);

 

                if (System.Text.RegularExpressions.Regex.IsMatch(ss, check, System.Text.RegularExpressions.RegexOptions.IgnoreCase))

                {

                    System.Console.WriteLine("  (match for '{0}' found)", check);

                }

                else

                {

                    System.Console.WriteLine();

                }

            }

 

            // Keep the console window open in debug mode.

            System.Console.WriteLine("Press any key to exit.");

            System.Console.ReadKey();

 

        }

    }

}

Output:

Image27.jpg

Ask Your Question

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.