test123
test123

Reputation: 14185

C# Regex, Unrecognized escape sequence

I have strings in the following format

    _AUTO_(123,SomeString)

and I am trying to extract 123 from the above string using regex. The regex, that I am using is:

    const string pattern = @"\_AUTO\_\(?<number>(\d)+\,";
    foreach(Match match in Regex.Matches(line, pattern)) { 
       Console.WriteLine(match.Groups["number"].Value); 
    } 

I am getting Unrecognized escape sequence \_ error exception. Could someone please point me what I am doing wrong?

Thanks!

Upvotes: 8

Views: 10535

Answers (1)

Dan
Dan

Reputation: 10786

You don't need to escape the underscore (or, for that matter, the comma). Change your regex to:

@"_AUTO_\(?<number>(\d)+,"

Upvotes: 16

Related Questions