Regex.Match Method
Searches an input string for an occurrence of a regular expression and returns the precise result as a single Match object.
Overload List
Searches the specified input string for an occurrence of the regular expression specified in the Regex constructor.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Function Match(String) As Match
[C#] public Match Match(string);
[C++] public: Match* Match(String*);
[JScript] public function Match(String) : Match;
Searches the input string for an occurrence of a regular expression with a specified input string starting position.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Function Match(String, Integer) As Match
[C#] public Match Match(string, int);
[C++] public: Match* Match(String*, int);
[JScript] public function Match(String, int) : Match;
Searches the specified input string for an occurrence of the regular expression supplied in the pattern parameter.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function Match(String, String) As Match
[C#] public static Match Match(string, string);
[C++] public: static Match* Match(String*, String*);
[JScript] public static function Match(String, String) : Match;
Searches the input string for an occurrence of a regular expression with a specified input string starting position and input string length.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Function Match(String, Integer, Integer) As Match
[C#] public Match Match(string, int, int);
[C++] public: Match* Match(String*, int, int);
[JScript] public function Match(String, int, int) : Match;
Searches the input string for an occurrence of the regular expression supplied in a pattern parameter with matching options supplied in an options parameter.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function Match(String, String, RegexOptions) As Match
[C#] public static Match Match(string, string, RegexOptions);
[C++] public: static Match* Match(String*, String*, RegexOptions);
[JScript] public static function Match(String, String, RegexOptions) : Match;
Example
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of Match. For other examples that might be available, see the individual overload topics.
[Visual Basic] Dim text As String = "One car red car blue car" Dim pat As String = "(\w+)\s+(car)" ' Compile the regular expression. 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 While (m.Success) matchCount += 1 Console.WriteLine("Match" & (matchCount)) Dim i As Integer For i = 1 to 2 Dim g as Group = m.Groups(i) Console.WriteLine("Group" & i & "='" & g.ToString() & "'") Dim cc As CaptureCollection = g.Captures Dim j As Integer For j = 0 to cc.Count - 1 Dim c As Capture = cc(j) Console.WriteLine("Capture" & j & "='" & c.ToString() _ & "', Position=" & c.Index) Next j Next i m = m.NextMatch() End While [C#] 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) { Console.WriteLine("Match"+ (++matchCount)); for (int i = 1; i <= 2; i++) { Group g = m.Groups[i]; Console.WriteLine("Group"+i+"='" + g + "'"); CaptureCollection cc = g.Captures; for (int j = 0; j < cc.Count; j++) { Capture c = cc[j]; System.Console.WriteLine("Capture"+j+"='" + c + "', Position="+c.Index); } } m = m.NextMatch(); } [C++] String* text = S"One car red car blue car"; String* pat = S"(\\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) { Console::WriteLine(S"Match{0}", __box(++matchCount)); for (int i = 1; i <= 2; i++) { Group* g = m->Groups->Item[i]; Console::WriteLine(S"Group{0}='{1}'", __box(i), g); CaptureCollection* cc = g->Captures; for (int j = 0; j < cc->Count; j++) { Capture* c = cc->Item[j]; System::Console::WriteLine(S"Capture{0}='{1}', Position={2}", __box(j), c, __box(c->Index)); } } m = m->NextMatch(); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
See Also
Regex Class | Regex Members | System.Text.RegularExpressions Namespace