This documentation is archived and is not being maintained.

Regex.Replace Method (String, String, String)

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)

public static string Replace(
	string input,
	string pattern,
	string replacement


Type: System.String
The string to search for a match.
Type: System.String
The regular expression pattern to match.
Type: System.String
The replacement string.

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.


A regular expression parsing error occurred.


input, pattern, or replacement is null.

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 .NET Framework Regular Expressions and Regular Expression Language - Quick Reference. 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.


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.

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.

using System;
using System.Text.RegularExpressions;

public class Example
   public static void Main()
      string input = "This is   text with   far  too   much   " + 
      string pattern = "\\s+";
      string replacement = " ";
      string result = Regex.Replace(input, pattern, replacement);

      Console.WriteLine("Original String: {0}", input);
      Console.WriteLine("Replacement String: {0}", result);                             
// 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.

The following example uses the Replace(String, String, String) method to replace the local machine and drive names in a UNC path with a local file path. The regular expression uses the Environment.MachineName property to include the name of the local computer, and the Environment.GetLogicalDrives method to include the names of the logical drives. To run the example successfully, you should replace the literal string "MyMachine" with your local machine name.

using System;
using System.Text.RegularExpressions;

public class Example
   public static void Main()
      // Get drives available on local computer and form into a single character expression.
      string[] drives = Environment.GetLogicalDrives();
      string driveNames = String.Empty;
      foreach (string drive in drives)
         driveNames += drive.Substring(0,1);
      // Create regular expression pattern dynamically based on local machine information.
      string pattern = @"\\\\(?i:" + Environment.MachineName + @")(?:\.\w+)*\\((?i:[" + driveNames + @"]))\$";

      string replacement = "$1:";
      string[] uncPaths = { @"\\\C$\ThingsToDo.txt", 
                            @"\\MyMachine\d$\documents\mydocument.docx" }; 

      foreach (string uncPath in uncPaths)
         Console.WriteLine("Input string: " + uncPath);
         Console.WriteLine("Returned string: " + Regex.Replace(uncPath, pattern, replacement));
// The example displays the following output if run on a machine whose name is
// MyMachine:
//    Input string: \\\C$\ThingsToTo.txt
//    Returned string: C:\ThingsToDo.txt
//    Input string: \\MyMachine\c$\ThingsToDo.txt
//    Returned string: c:\ThingsToDo.txt
//    Input string: \\MyMachine\d$\documents\mydocument.docx
//    Returned string: d:\documents\mydocument.docx

The regular expression pattern is defined by the following expression:

"\\\\(?i:" + Environment.MachineName + ")(?:\.\w+)*\\((?i:[" + driveNames + "]))\$"

The following table shows how the regular expression pattern is interpreted.




Match two consecutive backslash (\) characters. Because the backslash character is interpreted as the escape character, each backslash must be escaped with another backslash.

(?i:" + Environment.MachineName + ")

Perform a case-insensitive match of the string that is returned by the Environment.MachineName property.


Match the period (.) character followed by one or more word characters. This match can occur zero or more times. The matched subexpression is not captured.


Match a backslash (\) character.

((?i:[" + driveNames + "]))

Perform a case-insensitive match of the character class that consists of the individual drive lettters. This match is the first captured subexpression.


Match the literal dollar sign ($) character.

The replacement pattern $1 replaces the entire match with the first captured subexpression. That is, it replaces the UNC machine and drive name with the drive letter.

.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.