Compartir a través de


Regex.Match Method (String)

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

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

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

Syntax

'Declaration
Public Function Match ( _
    input As String _
) As Match
public Match Match(
    string input
)

Parameters

Return Value

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

Exceptions

Exception Condition
ArgumentNullException

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

Remarks

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 NextMatch method. You can also retrieve all matches in a single method call by calling the Regex.Matches(String) method.

Examples

The following example finds regular expression pattern matches in a string, then lists the matched groups, captures, and capture positions.

Imports System.Text.RegularExpressions
Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim text As String = "One car red car blue car"
      Dim pat As String = "(\w+)\s+(car)"

      ' Instantiate the regular expression object.
      Dim r As Regex = New Regex(pat, RegexOptions.IgnoreCase)

      ' Match the regular expression pattern against a text string.
      Dim m As Match = r.Match(text)
      Dim matchcount As Integer = 0
      Do While m.Success
         matchcount += 1
         outputBlock.Text += "Match" & (matchcount) & vbCrLf
         Dim i As Integer
         For i = 1 To 2
            Dim g As Group = m.Groups(i)
            outputBlock.Text += "Group" & i & "='" & g.ToString() & "'" & vbCrLf
            Dim cc As CaptureCollection = g.Captures
            Dim j As Integer
            For j = 0 To cc.Count - 1
               Dim c As Capture = cc(j)
               outputBlock.Text += "Capture" & j & "='" & c.ToString() _
                  & "', Position=" & c.Index & vbCrLf
            Next 
         Next 
         m = m.NextMatch()
      Loop
   End Sub
End Module
' This example displays the following output:
'       Match1
'       Group1='One'
'       Capture0='One', Position=0
'       Group2='car'
'       Capture0='car', Position=4
'       Match2
'       Group1='red'
'       Capture0='red', Position=8
'       Group2='car'
'       Capture0='car', Position=12
'       Match3
'       Group1='blue'
'       Capture0='blue', Position=16
'       Group2='car'
'       Capture0='car', Position=21
using System;
using System.Text.RegularExpressions;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string text = "One car red car blue car";
      string pat = @"(\w+)\s+(car)";
      // Compile the regular expression.
      Regex r = new Regex(pat, RegexOptions.IgnoreCase);
      // Match the regular expression pattern against a text string.
      Match m = r.Match(text);
      int matchCount = 0;
      while (m.Success)
      {
         outputBlock.Text += "Match" + (++matchCount) + "\n";
         for (int i = 1; i <= 2; i++)
         {
            Group g = m.Groups[i];
            outputBlock.Text += "Group" + i + "='" + g + "'" + "\n";
            CaptureCollection cc = g.Captures;
            for (int j = 0; j < cc.Count; j++)
            {
               Capture c = cc[j];
               outputBlock.Text += "Capture" + j + "='" + c + "', Position=" + c.Index + "\n";
            }
         }
         m = m.NextMatch();
      }
   }
}
//This example displays the following output:
//       Match1
//       Group1='One'
//       Capture0='One', Position=0
//       Group2='car'
//       Capture0='car', Position=4
//       Match2
//       Group1='red'
//       Capture0='red', Position=8
//       Group2='car'
//       Capture0='car', Position=12
//       Match3
//       Group1='blue'
//       Capture0='blue', Position=16
//       Group2='car'
//       Capture0='car', Position=21

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.