C# Regex.IsMatch

Regex type

Regex.IsMatch tests for a matching pattern. It does not capture groups from this pattern. It just sees if the pattern exists in a valid form in the input string according to the pattern. And IsMatch is ideal for this purpose.

This C# example program uses Regex.IsMatch. This method tests for a matching pattern.

Example

Note

The Regex.IsMatch method can be called in a program that includes the "using System.Text.RegularExpressions" using directive at the top, or with the fully qualified name System.Text.RegularExpressions.Regex.IsMatch.

The static method has two overloads, the first receiving two parameters and the second receiving three parameters. Both overloads receive the input string that you want to search for matches in. The second parameter is the actual regular expression pattern. The third parameter is the RegexOptions enumerated type that can be used as an option mask. This example shows the simplest overload.

Program that uses Regex.IsMatch method [C#]

using System;
using System.Text.RegularExpressions;

class Program
{
    /// <summary>
    /// Test string using Regex.IsMatch static method.
    /// </summary>
    static bool IsValid(string value)
    {
	return Regex.IsMatch(value, @"^[a-zA-Z0-9]*$");
    }

    static void Main()
    {
	//
	// Test the strings with the IsValid method, which uses Regex.IsMatch.
	//
	Console.WriteLine(IsValid("dotnetperls0123"));
	Console.WriteLine(IsValid("DotNetPerls"));
	Console.WriteLine(IsValid(":-)"));
	// Console.WriteLine(IsValid(null)); // Throws an exception
    }
}

Output

True
True
False
Main method

Overview. The program text in the example defines the Program class with two enclosed static methods: the IsValid method, which internally invokes the Regex.IsMatch method, and the Main entry point. Control flow begins with the Main entry point and the result boolean value from the IsValid method is printed three times.

IsValid method body. The IsValid method in the Program class wraps the method call to the Regex.IsMatch method. This method can be considered a façade design pattern because it adds another level of abstraction to the regular expression.

Sorted letters: A through Z

Parameters. The Regex.IsMatch method receives two parameters. The regular expression parameter pattern specifies a string that must only contain the characters a through z, uppercase A through Z, and the ten digit characters. There must be zero or more of these characters.

Tip: The ^ caret symbol designates the start of the string.
The $ symbol designates the termination of the pattern.

Implementation

.NET Framework information

The Regex.IsMatch method and the instance IsMatch method both invoke the Run method in the Regex class when executed. The Run method is also executed when the Regex.Match method is executed. When you use the static Regex.IsMatch method, a new Regex is created in the same was as any instance Regex object is created. This instance is discarded at the end of the method and will later be cleaned up by the garbage collector.

Summary

The C# programming language

We looked at the Regex.IsMatch method in the .NET Framework using the C# programming language. This method condenses the return value of the Regex.Match method to a boolean value indicating whether the pattern was matched or not. The example showed a usage of the Regex.IsMatch method to validate a string of characters with a transliteration pattern.

Regex.Match Examples Regex Type
.NET