Match.Result Method

Updated: December 2009

Returns the expansion of the specified replacement pattern.

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

public virtual string Result(
	string replacement
)

Parameters

replacement
Type: System.String

The replacement pattern to use.

Return Value

Type: System.String
The expanded version of the replacement parameter.

ExceptionCondition
ArgumentNullException

replacement is null.

NotSupportedException

Expansion is not allowed for this pattern.

Whereas the Regex.Replace method replaces all matches in an input string with a specified replacement pattern, the Result method replaces a single match with a specified replacement pattern. Because it operates on an individual match, it is also possible to perform processing on the matched string before you call the Result method.

The replacement parameter is a standard regular expression replacement pattern. It can consist of literal characters and regular expression substitutions. For more information, see Substitutions.

The following example replaces the hyphens that begin and end a parenthetical expression with parentheses.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "--(.+?)--";
      string replacement = "($1)";
      string input = "He said--decisively--that the time--whatever time it was--had come.";
      foreach (Match match in Regex.Matches(input, pattern))
      {
         string result = match.Result(replacement);
         Console.WriteLine(result);
      }
   }
}
// The example displays the following output: 
//       (decisively) 
//       (whatever time it was)

The regular expression pattern --(.+?)-- is interpreted as shown in the following table.

Pattern

Description

--

Match two hyphens.

(.+?)

Match any character one or more times, but as few times as possible. This is the first capturing group.

--

Match two hyphens.

Note that the regular expression pattern --(.+?)-- uses the lazy quantifier +?. If the greedy quantifier + were used instead, the regular expression engine would find only a single match in the input string.

The replacement string ($1) replaces the match with the first captured group, which is enclosed in parentheses.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

December 2009

Expanded the Remarks section and added an example.

Information enhancement.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft