What characters need to be escaped in .NET Regex?

后端 未结 4 766
伪装坚强ぢ
伪装坚强ぢ 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: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.

提交回复
热议问题