What characters need to be escaped in .NET Regex?

后端 未结 4 745
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 03:18

In a .NET Regex pattern, what special characters need to be escaped in order to be used literally?

相关标签:
4条回答
  • 2020-12-15 03:29

    I think you can get the list of chars as

    List<char> chars = Enumerable.Range(0,65535)
                    .Where(i=>((char)i).ToString()!=Regex.Escape(((char)i).ToString()))
                    .Select(i=>(char)i)
                    .ToList();
    

    --

    \t\n\f\r#$()*+.?[\^{|
    
    0 讨论(0)
  • 2020-12-15 03:35

    Here is the list of characters that need to be escaped to use them as normal literals:

    1. Opening square bracket [
    2. Backslash \
    3. Caret ^
    4. Dollar sign $
    5. Period or dot .
    6. Vertical bar or pipe symbol |
    7. Question mark ?
    8. Asterisk or star *
    9. Plus sign +
    10. Opening round bracket ( and the closing round bracket )
    11. Opening curly bracket {
    12. Pound/Hash sign #

    These special characters are often called "metacharacters".

    But, I agree with Jon to use Regex.Escape instead of hardcoding these character in code.

    0 讨论(0)
  • 2020-12-15 03:38

    See the MSDN documentation here: http://msdn.microsoft.com/en-us/library/az24scfc.aspx#character_escapes

    The problem with a complete list is that it depends on context. For example . must be escaped, unless it is enclosed in brackets, as in [.]. ] technically does not need to be escaped, unless it is preceded by [. - has no special meaning, unless it's inside of brackets, as in [A-Z]. = has no special meaning unless it is preceded by ? as in (?=).

    0 讨论(0)
  • 2020-12-15 03:41

    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);
    }
    
    0 讨论(0)
提交回复
热议问题