无法识别的转义序列\ _。 C#正则表达式

时间:2018-02-21 22:16:01

标签: c# .net regex

我有以下正则表达式在https://regex101.com/中正常工作,因此我想在C#中使用它,但我收到错误Unrecognized escape sequence \_

^https:\/\/github\.com\/([a-zA-Z\-0-9]+)\/([a-zA-Z\-\_]+)\/commit\/([a-fA-F0-9]{40}),(.*),([0-9]+),([0-9]+)$

在C#中我这样做:

string regex = @"^https:\/\/github\.com\/([a-zA-Z\-0-9]+)\/([a-zA-Z\-\_]+)\/commit\/([a-fA-F0-9]{40}),(.*),([0-9]+),([0-9]+)$";

然后想做:

if (Regex.IsMatch(input, regex)) and so on. the error is here on that line.

我无法理解为什么在使用@

时出现错误

1 个答案:

答案 0 :(得分:0)

您不需要在C#正则表达式中转义下划线。错误来自正则表达式解析器。

你提到它在regex101.com上运行正常。也许仔细检查它是否以你期望的方式处理下划线。