Click to Rate and Give Feedback

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Developer's Guide
Character Escapes

Most of the important regular expression language operators are unescaped single characters. The escape character \ (a single backslash) signals to the regular expression parser that the character following the backslash is not an operator. For example, the parser treats an asterisk (*) as a repeating quantifier and a backslash followed by an asterisk (\*) as the Unicode character 002A.

The character escapes listed in this table are recognized both in regular expressions and in replacement patterns.

Escaped character

Description

ordinary characters

Characters other than . $ ^ { [ ( | ) * + ? \ match themselves.

\a

Matches a bell (alarm) \u0007.

\b

Matches a backspace \u0008 if in a [] character class; otherwise, see the note following this table.

\t

Matches a tab \u0009.

\r

Matches a carriage return \u000D. Note that \r is not equivalent to the newline character, \n.

\v

Matches a vertical tab \u000B.

\f

Matches a form feed \u000C.

\n

Matches a new line \u000A.

\e

Matches an escape \u001B.

\040

Matches an ASCII character as octal (up to three digits); numbers with no leading zero are backreferences if they have only one digit or if they correspond to a capturing group number. (For more information, see Backreferences.) For example, the character \040 represents a space.

\x20

Matches an ASCII character using hexadecimal representation (exactly two digits).

\cC

Matches an ASCII control character; for example, \cC is control-C.

\u0020

Matches a Unicode character using hexadecimal representation (exactly four digits).

NoteNote:
The Perl 5 character escape that is used to specify Unicode is not supported by the .NET Framework. The Perl 5 character escape is of the form \x{####…}, where "####…" is a series of hexadecimal digits. Instead, use the .NET Framework character escape described in this row.

\

When followed by a character that is not recognized as an escaped character, matches that character. For example, \* is the same as \x2A.

NoteNote:

The escaped character \b is a special case. In a regular expression, \b denotes a word boundary (between \w and \W characters) except within a [] character class, where \b refers to the backspace character. In a replacement pattern, \b always denotes a backspace.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Character escapes not recognized in replacement patterns      ChernoJ   |   Edit   |   Show History
Contrary to the documentation here, character escapes in replacement patterns don't get translated to the corresponding character upon replace.

Eg:

Regex.Replace("abcDEFghi", "(DEF)", @"\t")

Expected: abc ghi

Actual: abc\tghi

Repro's on Visual Studio 2008, C#, .Net 3.5.
String Literals      CLovegren   |   Edit   |   Show History
From http://msdn.microsoft.com/en-us/library/aa691090(VS.71).aspx:
A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim

string c = "hello \t world";               // hello     world
string d = @"hello \t world";               // hello \t world

Drop the at (@) from your code and you should be fine.

using System.Text.RegularExpressions;
class test {
static void Main() {
System.Console.WriteLine( Regex.Replace( "abcDEFghi", "(DEF)", "\t" ) );
}
}

Tags What's this?: Add a tag
Flag as ContentBug
Not a string literals issue...      ChernoJ   |   Edit   |   Show History

Dropping the @ would be fine if I wanted a tab character embedded in the string. However, this documentation suggests that the '\' character followed by the 't' character inside a replacement pattern has special meaning, but it doesn't treat it that way.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker