Επεξεργασία

Κοινή χρήση μέσω


Regex.IsMatch Method

Definition

Indicates whether the regular expression finds a match in the input string.

Overloads

IsMatch(String, String, RegexOptions, TimeSpan)

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

IsMatch(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)

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

IsMatch(String, String, RegexOptions)

Indicates whether the specified regular expression finds a match in the specified input string, using the specified matching options.

IsMatch(ReadOnlySpan<Char>, String, RegexOptions)

Indicates whether the specified regular expression finds a match in the specified input span, using the specified matching options.

IsMatch(String, String)

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

IsMatch(ReadOnlySpan<Char>, Int32)

Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input span.

IsMatch(ReadOnlySpan<Char>, String)

Indicates whether the specified regular expression finds a match in the specified input span.

IsMatch(String, Int32)

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.

IsMatch(String)

Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input string.

IsMatch(ReadOnlySpan<Char>)

Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input span.

IsMatch(String, String, RegexOptions, TimeSpan)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

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

public:
 static bool IsMatch(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options, TimeSpan matchTimeout);
public static bool IsMatch (string input, string pattern, System.Text.RegularExpressions.RegexOptions options, TimeSpan matchTimeout);
static member IsMatch : string * string * System.Text.RegularExpressions.RegexOptions * TimeSpan -> bool
Public Shared Function IsMatch (input As String, pattern As String, options As RegexOptions, matchTimeout As TimeSpan) As Boolean

Parameters

input
String

The string to search for a match.

pattern
String

The regular expression pattern to match.

options
RegexOptions

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

matchTimeout
TimeSpan

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

Returns

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

Exceptions

A regular expression parsing error occurred.

input or pattern is null.

options is not a valid RegexOptions value.

-or-

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

A time-out occurred.

Examples

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.

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
    {
        bool isMatch = Regex.IsMatch(partNumber, pattern, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(500));
        Console.WriteLine($"{partNumber} {(isMatch ? "is" : "is not")} a valid part number.");
    }
    catch (RegexMatchTimeoutException e)
    {
        Console.WriteLine($"Timeout after {e.MatchTimeout} seconds matching {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.
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-Z0-9]\d{2}[A-Z0-9](-\d{3}){2}[A-Z0-9]$"
      For Each partNumber As String In partNumbers
         Try
            Console.WriteLine("{0} {1} a valid part number.", 
                              partNumber, _
                              IIF(Regex.IsMatch(partNumber, pattern, RegexOptions.IgnoreCase), _
                                  "is", "is not"),
                              TimeSpan.FromMilliseconds(500))
         Catch e As RegexMatchTimeoutException
            Console.WriteLine("Timeout after {0} seconds matching {1}.",
                              e.MatchTimeout, e.Input)
         End Try
      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.

The regular expression pattern is:

^[A-Z0-9]\d{2}[A-Z0-9](-\d{3}){2}[A-Z0-9]$

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

Pattern Description
^ Begin the match at the beginning of the string.
[A-Z0-9] Match any single alphabetic character from A through Z, or any numeric character.
\d{2} Match two numeric characters.
[A-Z0-9] Match any single alphabetic character from A through Z, or any 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-Z0-9] 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:

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

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

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. 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 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 and Backtracking. 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 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.

See also

Applies to

IsMatch(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

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

public:
 static bool IsMatch(ReadOnlySpan<char> input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options, TimeSpan matchTimeout);
public static bool IsMatch (ReadOnlySpan<char> input, string pattern, System.Text.RegularExpressions.RegexOptions options, TimeSpan matchTimeout);
static member IsMatch : ReadOnlySpan<char> * string * System.Text.RegularExpressions.RegexOptions * TimeSpan -> bool
Public Shared Function IsMatch (input As ReadOnlySpan(Of Char), pattern As String, options As RegexOptions, matchTimeout As TimeSpan) As Boolean

Parameters

input
ReadOnlySpan<Char>

The span to search for a match.

pattern
String

The regular expression pattern to match.

options
RegexOptions

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

matchTimeout
TimeSpan

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

Returns

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

Exceptions

A regular expression parsing error occurred.

pattern is null

A time-out occurred.

options is not in a valid RegexOptions value or matchTimeout is negative, zero, or greater than approximately 24 days.

Applies to

IsMatch(String, String, RegexOptions)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

Indicates whether the specified regular expression finds a match in the specified input string, using the specified matching options.

public:
 static bool IsMatch(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options);
public static bool IsMatch (string input, string pattern, System.Text.RegularExpressions.RegexOptions options);
static member IsMatch : string * string * System.Text.RegularExpressions.RegexOptions -> bool
Public Shared Function IsMatch (input As String, pattern As String, options As RegexOptions) As Boolean

Parameters

input
String

The string to search for a match.

pattern
String

The regular expression pattern to match.

options
RegexOptions

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

Returns

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

Exceptions

A regular expression parsing error occurred.

input or pattern is null.

options is not a valid RegexOptions value.

A time-out occurred. For more information about time-outs, see the Remarks section.

Examples

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.

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)
    Console.WriteLine("{0} {1} a valid part number.",
                      partNumber,
                      Regex.IsMatch(partNumber, pattern, RegexOptions.IgnoreCase) ? "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.
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-Z0-9]\d{2}[A-Z0-9](-\d{3}){2}[A-Z0-9]$"
      For Each partNumber As String In partNumbers
         Console.WriteLine("{0} {1} a valid part number.", _
                           partNumber, _
                           IIF(Regex.IsMatch(partNumber, pattern, RegexOptions.IgnoreCase), _
                               "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.

The regular expression pattern is:

^[A-Z0-9]\d{2}[A-Z0-9](-\d{3}){2}[A-Z0-9]$

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

Pattern Description
^ Begin the match at the beginning of the string.
[A-Z0-9] Match any single alphabetic character from A through Z, or any numeric character.
\d{2} Match two numeric characters.
[A-Z0-9] Match any single alphabetic character from A through Z, or any 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-Z0-9] 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) method with the options parameter set to RegexOptions.IgnoreCase is equivalent to defining the following regular expression:

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

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

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. 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) 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 Regular Expressions and Regular Expression Language - Quick Reference.

The RegexMatchTimeoutException exception is thrown if the execution time of the matching operation exceeds the time-out interval specified for the application domain in which the method is called. If no time-out is defined in the application domain's properties, or if the time-out value is Regex.InfiniteMatchTimeout, no exception is thrown.

Notes to Callers

This method times out after an interval that is equal to the default time-out value of the application domain in which it is called. If a time-out value has not been defined for the application domain, the value InfiniteMatchTimeout, which prevents the method from timing out, is used. The recommended static method for verifying a pattern match is IsMatch(String, String, RegexOptions, TimeSpan), which lets you set the time-out interval.

See also

Applies to

IsMatch(ReadOnlySpan<Char>, String, RegexOptions)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

Indicates whether the specified regular expression finds a match in the specified input span, using the specified matching options.

public:
 static bool IsMatch(ReadOnlySpan<char> input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options);
public static bool IsMatch (ReadOnlySpan<char> input, string pattern, System.Text.RegularExpressions.RegexOptions options);
static member IsMatch : ReadOnlySpan<char> * string * System.Text.RegularExpressions.RegexOptions -> bool
Public Shared Function IsMatch (input As ReadOnlySpan(Of Char), pattern As String, options As RegexOptions) As Boolean

Parameters

input
ReadOnlySpan<Char>

The span to search for a match.

pattern
String

The regular expression pattern to match.

options
RegexOptions

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

Returns

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

Exceptions

A regular expression parsing error occurred.

pattern is null

A time-out occurred.

options is not in a valid RegexOptions value.

Applies to

IsMatch(String, String)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

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

public:
 static bool IsMatch(System::String ^ input, System::String ^ pattern);
public static bool IsMatch (string input, string pattern);
static member IsMatch : string * string -> bool
Public Shared Function IsMatch (input As String, pattern As String) As Boolean

Parameters

input
String

The string to search for a match.

pattern
String

The regular expression pattern to match.

Returns

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

Exceptions

A regular expression parsing error occurred.

input or pattern is null.

A time-out occurred. For more information about time-outs, see the Remarks section.

Examples

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.

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($"{partNumber} {(Regex.IsMatch(partNumber, pattern) ? "is" : "is not")} " +
        $"a valid part number.");

// 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.
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.

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
^ Begin the match at the beginning of the line.
[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.

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. 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) 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 regular expression language elements that symbolically describe the string to match. For more information about regular expressions, see .NET Regular Expressions and Regular Expression Language - Quick Reference.

The RegexMatchTimeoutException exception is thrown if the execution time of the matching operation exceeds the time-out interval specified for the application domain in which the method is called. If no time-out is defined in the application domain's properties, or if the time-out value is Regex.InfiniteMatchTimeout, no exception is thrown.

Notes to Callers

This method times out after an interval that is equal to the default time-out value of the application domain in which the method is called. If a time-out value has not been defined for the application domain, the value InfiniteMatchTimeout, which prevents the method from timing out, is used. The recommended static method for verifying a pattern match is IsMatch(String, String, RegexOptions, TimeSpan), which lets you set the time-out interval.

See also

Applies to

IsMatch(ReadOnlySpan<Char>, Int32)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input span.

public:
 bool IsMatch(ReadOnlySpan<char> input, int startat);
public bool IsMatch (ReadOnlySpan<char> input, int startat);
member this.IsMatch : ReadOnlySpan<char> * int -> bool
Public Function IsMatch (input As ReadOnlySpan(Of Char), startat As Integer) As Boolean

Parameters

input
ReadOnlySpan<Char>

The span to search for a match.

startat
Int32

The zero-based character position at which to start the search.

Returns

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

Exceptions

A time-out occurred.

Applies to

IsMatch(ReadOnlySpan<Char>, String)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

Indicates whether the specified regular expression finds a match in the specified input span.

public:
 static bool IsMatch(ReadOnlySpan<char> input, System::String ^ pattern);
public static bool IsMatch (ReadOnlySpan<char> input, string pattern);
static member IsMatch : ReadOnlySpan<char> * string -> bool
Public Shared Function IsMatch (input As ReadOnlySpan(Of Char), pattern As String) As Boolean

Parameters

input
ReadOnlySpan<Char>

The span to search for a match.

pattern
String

The regular expression pattern to match.

Returns

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

Exceptions

A regular expression parsing error occurred.

pattern is null

A time-out occurred.

Applies to

IsMatch(String, Int32)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

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.

public:
 bool IsMatch(System::String ^ input, int startat);
public bool IsMatch (string input, int startat);
member this.IsMatch : string * int -> bool
Public Function IsMatch (input As String, startat As Integer) As Boolean

Parameters

input
String

The string to search for a match.

startat
Int32

The character position at which to start the search.

Returns

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

Exceptions

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.

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.

string[] partNumbers = [ "Part Number: 1298-673-4192", "Part No: A08Z-931-468A",
                      "_A90-123-129X", "123K-000-1230",
                      "SKU: 0919-2893-1256" ];
Regex rgx = MyRegex();
foreach (string partNumber in partNumbers)
{
    int start = partNumber.IndexOf(':');
    if (start >= 0)
    {
        Console.WriteLine($"{partNumber} {(rgx.IsMatch(partNumber, start) ? "is" : "is not")} a valid part number.");
    }
    else
    {
        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.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      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 
            Console.WriteLine("{0} {1} a valid part number.", _
                              partNumber, _
                              IIF(rgx.IsMatch(partNumber, start), "is", "is not"))
         Else
            Console.WriteLine("Cannot find starting position in {0}.", partNumber)
         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.

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.

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

For more details about startat, see the Remarks section of Match(String, Int32).

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.

See also

Applies to

IsMatch(String)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input string.

public:
 bool IsMatch(System::String ^ input);
public bool IsMatch (string input);
member this.IsMatch : string -> bool
Public Function IsMatch (input As String) As Boolean

Parameters

input
String

The string to search for a match.

Returns

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

Exceptions

input is null.

A time-out occurred. For more information about time-outs, see the Remarks section.

Examples

The following example illustrates the use of the IsMatch(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.

string[] partNumbers = [ "1298-673-4192", "A08Z-931-468A",
                      "_A90-123-129X", "12345-KKA-1230",
                      "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)
    Console.WriteLine($"{partNumber} {(rgx.IsMatch(partNumber) ? "is" : "is not")} a valid part number.");

// 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.
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 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
         Console.WriteLine("{0} {1} a valid part number.", _
                           partNumber, _
                           IIF(rgx.IsMatch(partNumber), "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.

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
^ Begin the match at the beginning of the line.
[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.

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

See also

Applies to

IsMatch(ReadOnlySpan<Char>)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input span.

public:
 bool IsMatch(ReadOnlySpan<char> input);
public bool IsMatch (ReadOnlySpan<char> input);
member this.IsMatch : ReadOnlySpan<char> -> bool
Public Function IsMatch (input As ReadOnlySpan(Of Char)) As Boolean

Parameters

input
ReadOnlySpan<Char>

The span to search for a match.

Returns

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

Exceptions

A time-out occurred.

Applies to