This documentation is archived and is not being maintained.

Regex.Escape Method

Escapes a minimal set of metacharacters (\, *, +, ?, |, {, [, (,), ^, $, ., #, and white space) by replacing them with their escape codes.

[Visual Basic]
Public Shared Function Escape( _
   ByVal str As String _
) As String
public static string Escape(
 string str
public: static String* Escape(
 String* str
public static function Escape(
   str : String
) : String;


The input string containing the text to convert.

Return Value

A string of characters with any metacharacters converted to their escaped form.


Escape converts a string so it can be used as a constant within a regular expression. (The number sign (#) and white-space characters must be escaped so the string can be used within an expression parsed with the IgnorePatternWhitespace option enabled. If future .NET regular expression features add additional metacharacters, Escape converts those characters to escaped form as well.)


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Regex Class | Regex Members | System.Text.RegularExpressions Namespace