Regex.Match Method (String, String, RegexOptions)

Searches the input string for the first occurrence of the specified regular expression, using the specified matching options.

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

public static Match Match(
	string input,
	string pattern,
	RegexOptions options
)

Parameters

input
Type: System.String
The string to search for a match.
pattern
Type: System.String
The regular expression pattern to match.
options
Type: System.Text.RegularExpressions.RegexOptions
A bitwise combination of the enumeration values that provide options for matching.

Return Value

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

ExceptionCondition
ArgumentException

A regular expression parsing error occurred.

ArgumentNullException

input or pattern is null.

ArgumentOutOfRangeException

options is not a valid bitwise combination of RegexOptions values.

The static Match(String, String, RegexOptions) method is equivalent to constructing a Regex object with the Regex(String, RegexOptions) constructor and calling the instance Match(String) method.

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 found 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, String, RegexOptions) method.

The following example defines a regular expression that matches words beginning with the letter "a". The RegexOptions.IgnoreCase option is used to ensure that the regular expression locates words beginning with both an uppercase "a" and a lowercase "a".


using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\ba\w*\b";
      string input = "An extraordinary day dawns with each new day.";
      Match m = Regex.Match(input, pattern, RegexOptions.IgnoreCase);
      if (m.Success)
         Console.WriteLine("Found '{0}' at position {1}.", m.Value, m.Index);
   }
}
// The example displays the following output:
//        Found 'An' at position 0.


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

Pattern

Description

\b

Begin the match at a word boundary.

a

Match the character "a".

\w*

Match zero, one, or more word characters.

\b

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.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft