4
string aniPattern=@"(?si:<option value=\\\"(?<year>.*?)\\)";

这会中断,因为 " 在中间。但我需要它,因为我在正则表达式中使用它。

我尝试使用string aniPattern="(?si:<option value=\\\"(?<year>.*?)\\\\)";(不带@),但它不是有效的正则表达式。

4

3 回答 3

12

important - it isn't entirely clear what you want to match; I've answered on the premise that only the " is being a problem - but see also Mike Caron's answer which assumes everything is escaped incorrectly.

With a verbatim string literal (i.e. @"..."), " is escaped to "" - so your string becomes:

string aniPattern=@"(?si:<option value=\\\""(?<year>.*?)\\)";

With a regular string literal (without the leading @), you would need a lot worse:

string aniPattern="(?si:<option value=\\\\\\\"(?<year>.*?)\\\\)";
于 2010-12-20T19:58:45.990 回答
8
string aniPattern=@"(?si:<option value=""(?<year>.*?)\)";

For @ escaped strings, you double the quotation mark to escape it, since backslash is not used.

于 2010-12-20T19:59:49.270 回答
2

Use two double quotes next to each other, like so: ""

于 2010-12-20T19:59:16.513 回答