What characters need to be escaped in .NET Regex?

I don’t know the complete set of characters – but I wouldn’t rely on the knowledge anyway, and I wouldn’t put it into code. Instead, I would use Regex.Escape whenever I wanted some literal text that I wasn’t sure about:

// Don't actually do this to check containment... it's just a little example.
public bool RegexContains(string haystack, string needle)
{
    Regex regex = new Regex("^.*" + Regex.Escape(needle) + ".*$");
    return regex.IsMatch(haystack);
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)