Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Regex Class
Regex Methods
IsMatch Method
 IsMatch Method (String, String)

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Regex..::.IsMatch Method (String, String)

Updated: March 2009

Indicates whether the regular expression finds a match in the input string using the regular expression specified in the pattern parameter.

Namespace:  System.Text.RegularExpressions
Assembly:  System (in System.dll)
Visual Basic (Declaration)
Public Shared Function IsMatch ( _
    input As String, _
    pattern As String _
) As Boolean
Visual Basic (Usage)
Dim input As String
Dim pattern As String
Dim returnValue As Boolean

returnValue = Regex.IsMatch(input, _
    pattern)
C#
public static bool IsMatch(
    string input,
    string pattern
)
Visual C++
public:
static bool IsMatch(
    String^ input, 
    String^ pattern
)
JScript
public static function IsMatch(
    input : String, 
    pattern : String
) : boolean

Parameters

input
Type: System..::.String
The string to search for a match.
pattern
Type: System..::.String
The regular expression pattern to match.

Return Value

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

A regular expression parsing error has occurred.

ArgumentNullException

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

-or-

pattern is nullNothingnullptra null reference (Nothing in Visual Basic).

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.

The static IsMatch(String, String) method is equivalent to constructing a Regex object with the regular expression pattern specified by pattern 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 various 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 Elements.

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.

Visual Basic
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim partNumbers() As String = { "1298-673-4192", "A08Z-931-468A", _
                                      "_A90-123-129X", "12345-KKA-1230", _
                                      "0919-2893-1256" }
      Dim pattern As String = "[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]"
      For Each partNumber As String In partNumbers
         Console.WriteLine("{0} {1} a valid part number.", _
                           partNumber, _
                           IIF(Regex.IsMatch(partNumber, pattern), "is", "is not"))
      Next
   End Sub
End Module
' 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.

C#
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-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]";
      foreach (string partNumber in partNumbers)
         Console.WriteLine("{0} {1} a valid part number.", 
                           partNumber, 
                           Regex.IsMatch(partNumber, pattern) ? "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-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.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

March 2009

Updated exception information.

Customer feedback.

February 2009

Expanded the Remarks section and added an example.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker