Regex.Match Method (String, String)
Searches the specified input string for the first occurrence of the specified regular expression.
Assembly: System (in System.dll)
Parameters
- input
- Type: System.String
The string to search for a match.
- pattern
- Type: System.String
The regular expression pattern to match.
Return Value
Type: System.Text.RegularExpressions.MatchAn object that contains information about the match.
| Exception | Condition |
|---|---|
| ArgumentException |
A regular expression parsing error occurred. |
| ArgumentNullException |
input or pattern is null. |
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 Elements.
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.
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.