Share via


Escape Method

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space) by replacing them with their escape codes. This instructs the regular expression engine to interpret these characters literally rather than as metacharacters.

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

Syntax

'Declaration
Public Shared Function Escape ( _
    input As String _
) As String
public static string Escape(
    string input
)
public:
static String^ Escape(
    String^ input
)
static member Escape : 
        input:string -> string 
public static function Escape(
    input : String
) : String

Parameters

Return Value

Type: System. . :: . .String
A string of characters with metacharacters converted to their escaped form.

Remarks

Escape converts a string so that the regular expression engine will interpret any metacharacters that it may contain as character literals. For example, consider a regular expression that is designed to extract comments that are delimited by straight opening and closing brackets ([ and ]) from text. In the following example, the regular expression "[(.*?)]" is interpreted as a character class. Rather than matching comments embedded in the input text, the regular expression matches each opening or closing parenthesis, period, asterisk, or question mark.

However, if the opening bracket is escaped by passing it to the Escape method, the regular expression succeeds in matching comments that are embedded in the input string.

In a regular expression that is defined by using static text, characters that are to be interpreted literally rather than as metacharacters can be escaped by preceding them with a backslash symbol (\) as well as by calling the Escape method. In a regular expression that is defined dynamically using characters that are not known at design time, calling the Escape method is particularly important to ensure that the regular expression engine interprets individual characters as literals rather than as metacharacters.

Note

If a regular expression pattern includes either the number sign (#) or literal white-space characters, they must be escaped if input text is parsed with the RegexOptions..::..IgnorePatternWhitespace option enabled.

While the Escape method escapes the straight opening bracket ([) and opening brace ({) characters, it does not escape their corresponding closing characters (] and }). In most cases, escaping these is not necessary. If a closing bracket or brace is not preceded by its corresponding opening character, the regular expression engine interprets it literally. If an opening braket or brace is interpreted as a metacharacter, the regular expression engine interprets the first corresponding closing character as a metacharacter. If this is not the desired behavior, the closing bracket or brace should be escaped by explicitly prepending the backslash (\) character.

.NET Framework Security

See Also

Reference

Regex Class

System.Text.RegularExpressions Namespace