2

I have the following regex which is working ok in https://regex101.com/ and so I want to use it in C# but I get the error Unrecognized escape sequence \_.

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

in C# I do that:

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

and then want to do:

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

I cant understand why I get the error in case I use @

ctwheels
  • 21,901
  • 9
  • 42
  • 77
Georgi
  • 107
  • 1
  • 6
  • 1
    Dupe of [What special characters must be escaped in regular expressions?](https://stackoverflow.com/questions/399078/what-special-characters-must-be-escaped-in-regular-expressions) or [C# Regex, Unrecognized escape sequence](https://stackoverflow.com/questions/8057914/c-sharp-regex-unrecognized-escape-sequence) – Wiktor Stribiżew Feb 21 '18 at 22:17

1 Answers1

4

You don't need to escape underscores in C# regular expressions. The error is coming from the regular expression parser.

You mentioned it is working OK on regex101.com. Maybe double check that it is dealing with underscores in the way you expect.

cbp
  • 25,252
  • 29
  • 125
  • 205