Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Regex.Replace Method (String, String, String)

In 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)
static member Replace : 
        input:string * 
        pattern:string * 
        replacement:string -> string


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 the replacement string takes the place of each matched string. If pattern is not matched in the current instance, the method returns the current instance unchanged.

A regular expression parsing error occurred.


input, pattern, or replacement is a null reference (Nothing in Visual Basic).



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



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 RegexMatchTimeoutException exception is thrown if the execution time of the replacement operation exceeds the time-out interval specified for the application domain in which the method is called. If no time-out is defined in the application domain's properties, or if the time-out value is Regex.InfiniteMatchTimeout, no exception is thrown.

Because the method returns input unchanged if there is no match, you can use the Object.ReferenceEquals method to determine whether the method has made any replacements to the input string.

Notes to Callers

This method times out after an interval that is equal to the default time-out value of the application domain in which it is called. If a time-out value has not been defined for the application domain, the value Regex.InfiniteMatchTimeout, which prevents the method from timing out, is used. The recommended static method for replacing a pattern match is Regex.Replace(String, String, String, RegexOptions, TimeSpan), which lets you set the time-out interval.

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.


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.


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.


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


.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8



Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.