Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# string that has to contain quote "

Tags:

c#

regex

quote

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

This breakes because the " in the middle. But I need that because I use it in a regex.

I tried to use string aniPattern="(?si:<option value=\\\"(?<year>.*?)\\\\)";(without @) but it isnot a valid regex.

like image 496
Ryan Avatar asked Nov 29 '22 05:11

Ryan


2 Answers

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>.*?)\\\\)";
like image 101
Marc Gravell Avatar answered Dec 16 '22 08:12

Marc Gravell


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

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

like image 28
Mike Caron Avatar answered Dec 16 '22 09:12

Mike Caron