2

I'm doing a match comparison on some escaped strings:

Regex.IsMatch("\\Application.evtx", "DebugLogs\\ConfigurationServices.log");

I don't see why I'm getting:

"parsing "DebugLogs\ConfigurationServices.log" - Unrecognized escape sequence \C."

The \C is escaped?

Darkenor
  • 4,349
  • 8
  • 40
  • 67
  • See http://stackoverflow.com/questions/1302864/unrecognized-escape-sequence-for-string-containing-backslashes-in-c-sharp - effectively, you need to escape the backslash, which is in itself an escape character - "DebugLogs\\ConfigurationServices.log" or @"DebugLogs\ConfigurationServices.log" - I'm voting to close this not because it's a bad question, but because it's already been covered on SO – dash May 02 '12 at 22:08
  • Apologies to everyOne but someone edited my post...I had two backslashes on each and the person converted it to one. I think it was too add syntax highlighting or some nonsense... – Darkenor May 03 '12 at 03:11
  • sorry, it's because you are actually feeding the string into a RegEx - so it sees the RegEx "DebugLogs\ConfigurationServices.log" and treats \C as an escape sequence (which it isn't, hence the error). Changing your strings to Regex.IsMatch("\\\\Application.evtx", "DebugLogs\\\\ConfigurationServices.log"); escapes the backslash in the RegEx, forcing it to treat it as a backslash and not as an escape sequence :-) It's essentially because backslash has a special meaning in a regular expression, so we escape it to remove that meaning. – dash May 03 '12 at 08:03

3 Answers3

4

The edit really fooled a lot of people, including me!

'\' is a special character in regular expressions - it effectively is an escape character or denotes an escape sequence.

So the RegEx engine sees DebugLogs*\C*onfigurationServices.log which is, indeed, an unrecognized escape sequence. \A actually is an existing escape sequence.

So you need to escape the escape character. The simplest way to do this is to double the number of slashes used:

Regex.IsMatch("\\\\Application.evtx", "DebugLogs\\\\ConfigurationServices.log");

Which the RegEx engine will see as comparisons betweeen "\\Appplication.evtx" and "DebugLogs\\ConfigurationServices.log" - now the backslash has been escaped and has no special meaning.

Regex.IsMatch(@"\\Application.evtx", @"DebugLogs\\ConfigurationServices.log"); 

works fine too and is more readable.

dash
  • 89,546
  • 4
  • 51
  • 71
1

The \ character is the escape character in strings. For example if you'd like to do a carriage return, you'd use \r. To get around this either use literal strings

@"\Application.evtx"

Or escape the escape character

"\\Application.evtx"
Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
-2

You probably want

Regex.IsMatch(@"\Application.evtx", @"DebugLogs\ConfigurationServices.log");

without the "@" C# will treat \C as an escape sequence similar to the way it would convert \n in to a newline character however \C is not a recognised/valid escape sequence.

phuzi
  • 12,078
  • 3
  • 26
  • 50