Regex escape with \ or \\?

前端 未结 5 435
生来不讨喜
生来不讨喜 2020-12-03 23:21

Can someone explain to me when using regular expressions when a double backslash or single backslash needs to be used to escape a character?

A lot of references onli

5条回答
  •  眼角桃花
    2020-12-04 00:01

    \ Is also an escape character for string literals in c# so the first \ is escaping the second \ being passed to the method and the second one is escaping the . in the regex.

    Use:

    if (Regex.IsMatch(myString, @"SomeString\."))
    

    If you want to avoid double escaping.

提交回复
热议问题