Regex.IsMatch Method (String, Int32)


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

Indicates whether the regular expression specified in the Regex constructor finds a match in the specified input string, beginning at the specified starting position in the string.

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

public bool IsMatch(
	string input,
	int startat


Type: System.String

The string to search for a match.

Type: System.Int32

The character position at which to start the search.

Return Value

Type: System.Boolean

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

Exception Condition

input is null.


startat is less than zero or greater than the length of input.


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 RegexMatchTimeoutException exception is thrown if the execution time of the matching operation exceeds the time-out interval specified by the Regex.Regex(String, RegexOptions, TimeSpan) constructor. If you do not set a time-out interval when you call the constructor, the exception is thrown if the operation exceeds any time-out value established for the application domain in which the Regex object is created. If no time-out is defined in the Regex constructor call or in the application domain's properties, or if the time-out value is Regex.InfiniteMatchTimeout, no exception is thrown.

The following example illustrates the use of the IsMatch(String, Int32) method to determine whether a string is a valid part number. It searches for a part number that follows a colon (:) character in a string. The IndexOf(Char) method is used to determine the position of the colon character, which is then passed to the IsMatch(String, Int32) method. 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= { "Part Number: 1298-673-4192", "Part No: A08Z-931-468A", 
                              "_A90-123-129X", "123K-000-1230", 
                              "SKU: 0919-2893-1256" };
      Regex rgx = new Regex(@"[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$");
      foreach (string partNumber in partNumbers)
         int start = partNumber.IndexOf(':');
         if (start >= 0)
            Console.WriteLine("{0} {1} a valid part number.", 
                              rgx.IsMatch(partNumber, start) ? "is" : "is not");
            Console.WriteLine("Cannot find starting position in {0}.", partNumber);
// The example displays the following output:
//       Part Number: 1298-673-4192 is a valid part number.
//       Part No: A08Z-931-468A is a valid part number.
//       Cannot find starting position in _A90-123-129X.
//       Cannot find starting position in 123K-000-1230.
//       SKU: 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.




Match a single alphabetic character (a through z or A through Z) or numeric character.


Match two numeric characters.


Match a single alphabetic character (a through z or A through Z) or 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 a single alphabetic character (a through z or A through Z) or numeric character.


End the match at the end of the line.

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