Regex.IsMatch Method (String, String, RegexOptions, TimeSpan)

.NET Framework (current version)

The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Indicates whether the specified regular expression finds a match in the specified input string, using the specified matching options and time-out interval.

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

public static bool IsMatch(
	string input,
	string pattern,
	RegexOptions options,
	TimeSpan matchTimeout


Type: System.String

The string to search for a match.

Type: System.String

The regular expression pattern to match.

Type: System.Text.RegularExpressions.RegexOptions

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

Type: System.TimeSpan

A time-out interval, or Regex.InfiniteMatchTimeout to indicate that the method should not time out.

Return Value

Type: System.Boolean

true if the regular expression finds a match; otherwise, false.

Exception Condition

A regular expression parsing error occurred.


input or pattern is null.


options is not a valid RegexOptions value.


matchTimeout is negative, zero, or greater than approximately 24 days.


A time-out occurred.

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, TimeSpan) 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 matchTimeout parameter specifies how long a pattern matching method should try to find a match before it times out. Setting a time-out interval prevents regular expressions that rely on excessive backtracking from appearing to stop responding when they process input that contains near matches. For more information, see Best Practices for Regular Expressions in the .NET Framework and Backtracking in Regular Expressions. If no match is found in that time interval, the method throws a RegexMatchTimeoutException exception. matchTimeout overrides any default time-out value defined for the application domain in which the method executes.

Notes to Callers:

We recommend that you set the matchTimeout parameter to an appropriate value, such as two seconds. If you disable time-outs by specifying Regex.InfiniteMatchTimeout, the regular expression engine offers slightly better performance. However, you should disable time-outs only under the following conditions:

  • When the input processed by a regular expression is derived from a known and trusted source or consists of static text. This excludes text that has been dynamically input by users.

  • When the regular expression pattern has been thoroughly tested to ensure that it efficiently handles matches, non-matches, and near matches.

  • When the regular expression pattern contains no language elements that are known to cause excessive backtracking when processing a near match.

The following example illustrates the use of the IsMatch(String, String, RegexOptions, TimeSpan) 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. Matching the regular expression pattern should involve minimal searching through the input string, so the method sets a time-out interval of 500 milliseconds.

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)
         try {
            Console.WriteLine("{0} {1} a valid part number.", 
                              Regex.IsMatch(partNumber, pattern, RegexOptions.IgnoreCase) 
                                            ? "is" : "is not", TimeSpan.FromMilliseconds(500));
         catch (RegexMatchTimeoutException e) {
            Console.WriteLine("Timeout after {0} seconds matching {1}.",
                              e.MatchTimeout, e.Input);
// 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:


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




Begin the match at the beginning of the string.


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


Match two numeric characters.


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


Match a hyphen.


Match exactly three numeric characters.


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


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, TimeSpan) method with the options parameter set to RegexOptions.IgnoreCase is equivalent to defining the following regular expression:


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

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top