IsMatch method (String, String, RegexOptions)
. NET Framework class library for Silverlight Regex...:. IsMatch method (String, String, RegexOptions)
Indicates the regular expression usage.PatternThe Regular Expression andOptionsWhether the matching option provided by the parameter is found in the input string.
Namespace:System. Text. RegularExpressions
Assembly:System (in System. dll)
Syntax
Visual Basic (Declaration)
Public Shared Function IsMatch ( _ input As String, _ pattern As String, _ options As RegexOptions _) As Boolean
Visual Basic (usage)
Dim input As StringDim pattern As StringDim options As RegexOptionsDim returnValue As BooleanreturnValue = Regex.IsMatch(input, _ pattern, options)
C #
public static bool IsMatch( string input, string pattern, RegexOptions options)
Parameters
-
Input
-
Type: System...:. String
The string to search for matching items.
-
Pattern
-
Type: System...:. String
The regular expression pattern to be matched.
-
Options
-
Type: System. Text. RegularExpressions...:. RegexOptions
The bitwise combination of enumerated values.
Return Value
Type: System...:. Boolean
True if the regular expression finds a match. Otherwise, false.
Exception
Exception |
Condition |
ArgumentException |
Regular Expression Analysis Error. |
ArgumentNullException |
InputIs a nullNothingnullptrnull reference (Nothing in Visual Basic ). -Or- PatternIs a nullNothingnullptrnull reference (Nothing in Visual Basic ). |
ArgumentOutOfRangeException |
OptionsIt is not a valid RegexOptions value. |
Remarks
The IsMatch method is usually used to verify a string, or ensure that the string conforms to the specified mode for subsequent operations without retrieving the string. To determine whether one or more strings Match a regular expression pattern and retrieve these strings for subsequent operations, call the Match or Matches method.
StaticIsMatch (String, String, RegexOptions)The method is equivalentPatternThe specified regular expression mode andOptionsThe specified Regular Expression option constructs the Regex object and calls the IsMatch (String) instance method. This regular expression mode will be cached for quick retrieval by the Regular Expression Engine.
PatternA parameter is composed of various regular expression language elements that describe the string to be matched by a symbol. For more information about regular expressions, see. NET Framework regular expressions in the. NET Framework documentation and the theme of the Regular Expression Language element.
Example
The following example illustrates how to use the IsMatch (String, String) method to determine whether a String is a valid part number. This regular expression assumes that this part number has a specific format (consisting of three groups of characters separated by a hyphen ). The first group contains four characters, which must be a letter or digit followed by two digits followed by a letter or digit. The second group consists of three characters and must be a number. The third group consists of four characters, which must contain three digits followed by one letter or digit.
Visual Basic
Copy code
Imports System.Text.RegularExpressionsModule Example Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) Dim partNumbers() As String = {"1298-673-4192", "A08Z-931-468a", _ "_A90-123-129X", "12345-KKA-1230", _ "0919-2893-1256"} Dim pattern As String = "^[A-Z0-9]\d{2}[A-Z0-9](-\d{3}){2}[A-Z0-9]$" For Each partNumber As String In partNumbers outputBlock.Text += String.Format("{0} {1} a valid part number.", _ partNumber, _ IIf(Regex.IsMatch(partNumber, pattern, RegexOptions.IgnoreCase), _ "is", "is not")) & vbCrLf Next End SubEnd 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 #
Copy code
using System;using System.Text.RegularExpressions;public class Example{ public static void Demo(System.Windows.Controls.TextBlock outputBlock) { 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) outputBlock.Text += String.Format("{0} {1} a valid part number.", partNumber, Regex.IsMatch(partNumber, pattern, RegexOptions.IgnoreCase) ? "is" : "is not") + "\n"; }}// 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 mode is:
Copy code
^[A-Z0-9]\d{2}[A-Z0-9](-\d{3}){2}[A-Z0-9]$
The following table shows how to explain the regular expression mode.
Mode |
Description |
^ |
Starts from the beginning of the string. |
A-Z0-9 |
Match any single letter character (A to Z) or any number character. |
\ D {2} |
Match two numeric characters. |
A-Z0-9 |
Match any single letter character (A to Z) or any number character. |
- |
Match a hyphen. |
\ D {3} |
Matches exactly three numeric characters. |
(-\ D {3}) {2} |
Search for the mode in which a single character is followed by three numeric characters and match two matching items in this mode. |
A-Z0-9 |
Match any single letter character (A to Z) or any number character. |
$ |
End the match at the end of the string. |
InOptionsWhen the parameter is set to RegexOptions...:. IgnoreCaseIsMatch (String, String, RegexOptions)The method is equivalent to defining the following regular expressions:
Copy code
[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]
For comparison, see the IsMatch (String, String) method example.