我有以下正则表达式,它在 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.
我不明白为什么在使用 @
时会出现错误
最佳答案
您不需要在 C# 正则表达式中转义下划线。该错误来自正则表达式解析器。
您提到它在 regex101.com 上运行正常。也许仔细检查它是否按照您期望的方式处理下划线。
关于c# - 无法识别的转义序列\_。 C# 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48916435/