0

I was using Regex and I tried to write:

Regex RegObj2 = new Regex("\w[a][b][(c|d)][(c|d)].\w");

Gives me this error twice, one for each appearance of \w:

unrecognized escape sequence

What am I doing wrong?

gd1
  • 11,300
  • 7
  • 49
  • 88
  • 1
    possible duplicate of [C# Regex Issue "unrecognized escape sequence"](http://stackoverflow.com/questions/6155219/c-sharp-regex-issue-unrecognized-escape-sequence) – sloth Jan 25 '13 at 14:02
  • 1
    possible duplicate of [regex - unrecognized escape sequence](http://stackoverflow.com/questions/10318665/regex-unrecognized-escape-sequence) – sloth Jan 25 '13 at 14:02
  • possible duplicate of [How do I escape a RegEx?](http://stackoverflow.com/questions/10418008/how-do-i-escape-a-regex) – sloth Jan 25 '13 at 14:04
  • possible duplicate of [Simple regex pattern](http://stackoverflow.com/questions/623787/simple-regex-pattern) – sloth Jan 25 '13 at 14:05
  • Apart from the error in how you specify the string, I think you may have some problem with the regex. – nhahtdh Jan 25 '13 at 14:52

4 Answers4

4

You are not escaping the \s in a non-verbatim string literal.

Solution: put a @ in front of the string or double the backslashes, as per the C# rules for string literals.

Jon
  • 428,835
  • 81
  • 738
  • 806
3

Try to escape the escape ;)

Regex RegObj2 = new Regex("\\w[a][b][(c|d)][(c|d)].\\w");

or add a @ (as @Dominic Kexel suggested)

Davide Berra
  • 6,387
  • 2
  • 29
  • 50
1

There are two levels of potential escaping required when writing a regular expression:

  • The regular expression escaping (e.g. escaping brackets, or in this case specifying a character class)
  • The C# string literal escaping

In this case, it's the latter which is tripping you up. Either escape the \ so that it becomes part of the string, or use a verbatim string literal (with an @ prefix) so that \ doesn't have its normal escaping meaning. So either of these:

Regex regex1 = new Regex(@"\w[a][b][(c|d)][(c|d)].\w");
Regex regex2 = new Regex("\\w[a][b][(c|d)][(c|d)].\\w");

The two approaches are absolutely equivalent at execution time. In both cases you're trying to create a string constant with the value

\w[a][b][(c|d)][(c|d)].\w

The two forms are just different ways of expressing this in C# source code.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

The backslashes are not being escaped e.g. \\ or

new Regex(@"\w[a][b][(c|d)][(c|d)].\w");
DGibbs
  • 14,316
  • 7
  • 44
  • 83
  • Either escape the backslash or switch to a verbatim string, not both. `@"\\w"` tries to match the literal sequence `\w`. – Alan Moore Jan 25 '13 at 14:38