Share via


How to: Search Strings Using Regular Expressions (C# Programming Guide) 

The System.Text.RegularExpressions.Regex class can be used to search strings. These searches can range in complexity from very simple to making full use of regular expressions. The following are two examples of string searching using the Regex class. For more information, see .NET Framework Regular Expressions.

Example

The following code is a console application that performs a simple case insensitive search of the strings in an array. The static method System.Text.RegularExpressions.Regex.IsMatch(System.String,System.String,System.Text.RegularExpressions.RegexOptions) performs the search given the string to search and a string containing the search pattern. In this case, a third argument is used to indicate that case should be ignored. For more information, see System.Text.RegularExpressions.RegexOptions.

class TestRegularExpressions
{
    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

The following code is a console application that uses regular expressions to validate the format of each string in an array. The validation requires that each string take the form of a telephone number in which three groups of digits are separated by dashes, the first two groups contain three digits, and the third group contains four digits. This is accomplished with the regular expression ^\\d{3}-\\d{3}-\\d{4}$. For more information, see Regular Expression Language Elements.

class TestRegularExpressionValidation
{
    static void Main()
    {
        string[] numbers = 
        {
            "123-456-7890", 
            "444-234-22450", 
            "690-203-6578", 
            "146-893-232",
            "146-839-2322",
            "4007-295-1111", 
            "407-295-1111", 
            "407-2-5555", 
        };

        string sPattern = "^\\d{3}-\\d{3}-\\d{4}$";

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

            if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern))
            {
                System.Console.WriteLine(" - valid");
            }
            else
            {
                System.Console.WriteLine(" - invalid");
            }
        }
    }
}

Output

    123-456-7890 - valid
    444-234-22450 - invalid
    690-203-6578 - valid
    146-893-232 - invalid
    146-839-2322 - valid
    4007-295-1111 - invalid
    407-295-1111 - valid
    407-2-5555 - invalid

See Also

Concepts

C# Programming Guide

Other Resources

Strings (C# Programming Guide)