Okay, so how do i do it without an error? (Regex Literal Matching)

Something that should be incredibly simple.

I want to match a ?. A literal ?.

So… Regex.Replace(instring, "?"....
Flagged: “Regex issue: Quantifier {x,y} following nothing”.

Right, because ? is a special character. Fine. Escape it.

Regex.Replace(instring, "\?"....
Flagged: Unrecognized escape sequence.

…so how do i match a literal question mark?

Does this work with two backslashes?

regex.replace(instring, "\\?"....

Or maybe ??

Or you can use unicode e.g. \u003F for a question mark.

example here

Forest for the trees problem, mostly. I was looking in the wrong place.

"\\?" works (which led me to think of the fourth possibility),
"??" doesn’t because it still thinks the first ? is a quantifier for nothing…and even if it didn’t, the second ? would allow "" to be a match, because it would make the question mark optional. I suppose i could have used + if it worked, and just assumed there wouldnt be multiple ?'s in a row.
"\u003F" actually gets flagged as a quantifier still.
The first and third options point to the idea that the string is being interpreted before being used.

@"\?" (which is the first one, but forced into a Literal String, which is what I was forgetting) works as well. It’s the equivalent of the first option.

2 Likes