Regex.Replace Method (String, String, String)

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

Within a specified input string, replaces all strings that match a specified regular expression with a specified replacement string.

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

Syntax

'Declaration
Public Shared Function Replace ( _
    input As String, _
    pattern As String, _
    replacement As String _
) As String
public static string Replace(
    string input,
    string pattern,
    string replacement
)

Parameters

  • pattern
    Type: System.String
    The regular expression pattern to match.

Return Value

Type: System.String
A new string that is identical to the input string, except that a replacement string takes the place of each matched string.

Exceptions

Exception Condition
ArgumentException

A regular expression parsing error occurred.

ArgumentNullException

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

-or-

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

-or-

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

Remarks

The static Replace methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Replace.

The pattern parameter consists of various regular expression language elements that symbolically describe the string to match. For more information about regular expressions, see Regular Expression Language Elements in the .NET Framework documentation. The search for matches starts at the beginning of the input string.

The replacement parameter specifies the string that is to replace each match in input. replacement can consist of any combination of literal text and substitutions. For example, the replacement pattern a*${test}b inserts the string "a*" followed by the substring that is matched by the test capturing group, if any, followed by the string "b". The * character is not recognized as a metacharacter within a replacement pattern.

NoteNote:

Substitutions are the only regular expression language elements that are recognized in a replacement pattern. All other regular expression language elements, including character escapes, are allowed in regular expression patterns only and are not recognized in replacement patterns.

Examples

The following example defines a regular expression, \s+, that matches one or more white-space characters. The replacement string, " ", replaces them with a single space character.

Imports System.Text.RegularExpressions

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim input As String = "This is   text with   far  too   much   " + _
                            "whitespace."
      Dim pattern As String = "\s+"
      Dim replacement As String = " "
      Dim result As String = Regex.Replace(input, pattern, replacement)

      outputBlock.Text += String.Format("Original String: {0}", input) & vbCrLf
      outputBlock.Text += String.Format("Replacement String: {0}", result) & vbCrLf
   End Sub
End Module
' The example displays the following output:
'          Original String: This is   text with   far  too   much   whitespace.
'          Replacement String: This is text with far too much whitespace.
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string input = "This is   text with   far  too   much   " +
                     "whitespace.";
      string pattern = "\\s+";
      string replacement = " ";
      string result = Regex.Replace(input, pattern, replacement);

      outputBlock.Text += String.Format("Original String: {0}", input) + "\n";
      outputBlock.Text += String.Format("Replacement String: {0}", result) + "\n";
   }
}
// The example displays the following output:
//       Original String: This is   text with   far  too   much   whitespace.
//       Replacement String: This is text with far too much whitespace.

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.