Regex escape questionmark and double quotes
c#, escaping, regex
Solution
`?` and `.` are special chars for a regex, but can't be escaped "as is" in a string litteral. So if you put one `\`, it will be wrong for a string, and if you don't put `\\`, it will be taken as the "special char" of the regexp. So :
"@<a href=\"default\\.asp\\?itemID=([0-9]*)\">";
Problem
I have data with several occurrencies of the following string: ``` <a href="default.asp?itemID=987"> ``` in which the itemID is always different. I am using C# and I want to get all those itemIDs with a Regular Expression. At first I tried this ``` "<a href=\"default.asp?itemID=([0-9]*)\">" ``` But the questionmark is a reserved character. I considered using the @ operator to disable escaping of characters. But there are still some double quotes that really need escaping. So then I would go for ``` "<a href=\"default.asp\\?itemID=([0-9]*)\">" ``` which should be translated (as a string) to ``` <a href="default.asp\?itemID=([0-9]*)"> ``` But the Regex.Match method gets no success. I tried the very same regex here and it worked. What am I doing wrong?