There are two escaping mechanisms at work here, and they interfere. For example, you use \"
to tell C# to escape the following double quote, but you also use \w
to tell the regular expression parser to treat the following W special. But C# thinks \w
is meant for C#, doesn't understand it, and you get a compiler error.
For example take this example text:
<a href="file://C:\Test\Test2\[\w\.\/:]+">
There are two ways to escape it such that C# accepts it.
One way is to escape all characters that are special to C#. In this case the "
is used to denote the end of the string, and \
denotes a C# escape sequence. Both need to be prefixed with a C# escape \
to escape them:
string s = "<a href=\"file://C:\\Test\\Test2\\[\\w\\.\\/:]+\">";
But this often leads to ugly strings, especially when used with paths or regular expressions.
The other way is to prefix the string with @
and escape only the "
by replacing them with ""
:
string s = @"<a href=""file://C:\Test\Test2\[\w\.\/:]+"">";
The @
will prevent C# from trying to interpret the \
in the string as escape characters, but since \"
will not be recognized then either, they invented the ""
to escape the double quote.