Match Method (String, String)
Collapse the table of content
Expand the table of content

Regex.Match Method (String, String)

Searches the specified input string for the first occurrence of the specified regular expression.

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

Public Shared Function Match ( _
	input As String, _
	pattern As String _
) As Match


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

Return Value

Type: System.Text.RegularExpressions.Match
An object that contains information about the match.


A regular expression parsing error occurred.


input or pattern is Nothing.

The static Match(String, String) method is equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance Match(String) method. In this case, the regular expression engine caches the regular expression pattern.

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

You can determine whether the regular expression pattern has been found in the input string by checking the value of the returned Match object's Success property. If a match is successful, the returned Match object's Value property contains the substring from input that matches the regular expression pattern. If no match is found, its value is String.Empty.

This method returns the first substring in input that matches the regular expression pattern. You can retrieve subsequent matches by repeatedly calling the returned Match object's Match.NextMatch method. You can also retrieve all matches in a single method call by calling the Regex.Matches(String, String) method.

The following example calls the Match method to find the first word that contains at least one z character, and then calls the Match.NextMatch method to find any additional matches.

Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "ablaze beagle choral dozen elementary fanatic " +
                            "glaze hunger inept jazz kitchen lemon minus " +
                            "night optical pizza quiz restoration stamina " +
                            "train unrest vertical whiz xray yellow zealous"
      Dim pattern As String = "\b\w*z+\w*\b"
      Dim m As Match = Regex.Match(input, pattern)
      Do While m.Success 
         Console.WriteLine("'{0}' found at position {1}", m.Value, m.Index)
         m = m.NextMatch()
   End Sub
End Module
' The example displays the following output:
    'ablaze' found at position 0
    'dozen' found at position 21
    'glaze' found at position 46
    'jazz' found at position 65
    'pizza' found at position 104
    'quiz' found at position 110
    'whiz' found at position 157
    'zealous' found at position 174

The regular expression pattern \b\w*z+\w*\b is interpreted as shown in the following table.




Begin the match at a word boundary.


Match zero, one, or more word characters.


Match one or more occurrences of the z character.


Match zero, one, or more word characters.


End the match at a word boundary.

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

© 2016 Microsoft