Regex.IsMatch Method (String, Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Function IsMatch ( _
    input As String, _
    startat As Integer _
) As Boolean
public bool IsMatch(
    string input,
    int startat
)

Parameters

  • startat
    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.

Exceptions

Exception Condition
ArgumentNullException

input is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

startat cannot be less than zero or greater than the length of input.

Remarks

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. To determine whether one or more strings match a regular expression pattern and to retrieve them for subsequent manipulation, call the Match or Matches method.

Examples

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.

Imports System.Text.RegularExpressions

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim partNumbers() As String = {"Part Number: 1298-673-4192", "Part No: A08Z-931-468A", _
                                      "_A90-123-129X", "123K-000-1230", _
                                      "SKU: 0919-2893-1256"}
      Dim rgx As New Regex("[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$")
      For Each partNumber As String In partNumbers
         Dim start As Integer = partNumber.IndexOf(":"c)
         If start >= 0 Then
            outputBlock.Text += String.Format("{0} {1} a valid part number.", _
                              partNumber, _
                              IIf(rgx.IsMatch(partNumber, start), "is", "is not")) & vbCrLf
         Else
            outputBlock.Text += String.Format("Cannot find starting position in {0}.", partNumber) & vbCrLf
         End If
      Next
   End Sub
End Module
' 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.
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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)
         {
            outputBlock.Text += String.Format("{0} {1} a valid part number.",
                              partNumber,
                              rgx.IsMatch(partNumber, start) ? "is" : "is not") + "\n";
         }
         else
         {
            outputBlock.Text += String.Format("Cannot find starting position in {0}.", partNumber) + "\n";
         }
      }
   }
}
// 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:

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

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

Pattern

Description

[a-zA-Z0-9]

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

\d{2}

Match two numeric characters.

[a-zA-Z0-9]

Match a single alphabetic character (a through z or A through Z) or 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-zA-Z0-9]

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

$

End the match at the end of the line.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.