Regex.IsMatch Method (String, String, RegexOptions)

Indicates whether the specified regular expression finds a match in the specified input string, using the specified matching options.

Namespace:  System.Text.RegularExpressions
Assembly:  System (in System.dll)

public static bool IsMatch(
	string input,
	string pattern,
	RegexOptions options
)

Parameters

input
Type: System.String

The string to search for a match.

pattern
Type: System.String

The regular expression pattern to match.

options
Type: System.Text.RegularExpressions.RegexOptions

A bitwise combination of the enumeration values that provide options for matching.

Return Value

Type: System.Boolean
true if the regular expression finds a match; otherwise, false.

ExceptionCondition
ArgumentException

A regular expression parsing error occurred.

ArgumentNullException

input or pattern is null.

ArgumentOutOfRangeException

options is not a valid RegexOptions value.

RegexMatchTimeoutException

A time-out occurred. For more information about time-outs, see the Remarks section.

The IsMatch method is typically used to validate a string or to ensure that a string conforms to a particular pattern without retrieving that string for subsequent manipulation. If you want to determine whether one or more strings match a regular expression pattern and then retrieve them for subsequent manipulation, call the Match or Matches method.

The static IsMatch(String, String, RegexOptions) method is equivalent to constructing a Regex object with the regular expression pattern specified by pattern and the regular expression options specified by options and calling the IsMatch(String) instance method. This regular expression pattern is cached for rapid retrieval by the regular expression engine.

The pattern parameter consists of regular expression language elements that symbolically describe the string to match. For more information about regular expressions, see .NET Framework Regular Expressions and Regular Expression Language - Quick Reference.

The RegexMatchTimeoutException exception is thrown if the execution time of the matching operation exceeds the time-out interval specified for the application domain in which the method is called. If no time-out is defined in the application domain's properties, or if the time-out value is Regex.InfiniteMatchTimeout, no exception is thrown.

Notes to Callers

This method times out after an interval that is equal to the default time-out value of the application domain in which it is called. If a time-out value has not been defined for the application domain, the value InfiniteMatchTimeout, which prevents the method from timing out, is used. The recommended static method for verifying a pattern match is IsMatch(String, String, RegexOptions, TimeSpan), which lets you set the time-out interval.

The following example illustrates the use of the IsMatch(String, String) method to determine whether a string is a valid part number. The regular expression assumes that the part number has a specific format that consists of three sets of characters separated by hyphens. The first set, which contains four characters, must consist of an alphanumeric character followed by two numeric characters followed by an alphanumeric character. The second set, which consists of three characters, must be numeric. The third set, which consists of four characters, must have three numeric characters followed by an alphanumeric character.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string[] partNumbers= { "1298-673-4192", "A08Z-931-468a", 
                              "_A90-123-129X", "12345-KKA-1230", 
                              "0919-2893-1256" };
      string pattern = @"^[A-Z0-9]\d{2}[A-Z0-9](-\d{3}){2}[A-Z0-9]$";
      foreach (string partNumber in partNumbers)
         Console.WriteLine("{0} {1} a valid part number.", 
                           partNumber, 
                           Regex.IsMatch(partNumber, pattern, RegexOptions.IgnoreCase) 
                                         ? "is" : "is not");
   }
}
// The example displays the following output: 
//       1298-673-4192 is a valid part number. 
//       A08Z-931-468a is a valid part number. 
//       _A90-123-129X is not a valid part number. 
//       12345-KKA-1230 is not a valid part number. 
//       0919-2893-1256 is not a valid part number.

The regular expression pattern is:

^[A-Z0-9]\d{2}[A-Z0-9](-\d{3}){2}[A-Z0-9]$

The following table shows how the regular expression pattern is interpreted.

Pattern

Description

^

Begin the match at the beginning of the string.

[A-Z0-9]

Match any single alphabetic character from A through Z, or any numeric character.

\d{2}

Match two numeric characters.

[A-Z0-9]

Match any single alphabetic character from A through Z, or any numeric character.

-

Match a hyphen.

\d{3}

Match exactly three numeric characters.

(-\d{3}){2}

Find a hyphen followed by three numeric characters, and match two occurrences of this pattern..

[A-Z0-9]

Match any single alphabetic character from A through Z, or any numeric character.

$

End the match at the end of the string.

Calling the IsMatch(String, String, RegexOptions) method with the options parameter set to RegexOptions.IgnoreCase is equivalent to defining the following regular expression:

[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]

For comparison, see the example for the IsMatch(String, String) method.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft