I’m having trouble telling Regex to ignore any escape sequences.
Here is some example code:
string input = "?";
foreach (Match m in Regex.Matches(input, "?"))
{
...
}
But when it is executed, it throws the following error:
parsing “?” – Quantifier {x,y} following nothing.
I just want the “?” to be handled as a string.
Thanks.
EDIT:
I have also tried:
foreach (Match m in Regex.Matches(input, "\?"))
{
...
}
Which tells me that is is not recognized as a valid escape sequence.
I’ve also tried:
foreach (Match m in Regex.Matches(input, "\x3f"))
{
...
}
.NET provides a function that does any escaping for you automatically. Whenever you have some kind of input string, that you want to match literally (just the characters that are there), but you know you use a regex search, then run them through this method:
This will take care of any characters that might be meta-characters for regular expressions.
MSDN on
Escape