13

Can someone explain to me when using regular expressions when a double backslash or single backslash needs to be used to escape a character?

A lot of references online use a single backslash and online regex testers work with single backslashes, but in practice I often have to use a double backslash to escape a character.

For example:

"SomeString\."

Works in an online regex tester and matches "SomeString" followed by a dot.

However in practice I have to use a double escape:

if (Regex.IsMatch(myString, "SomeString\\."))
Duane
  • 1,980
  • 4
  • 17
  • 27
  • Although geared towards JavaScript, you may find [this answer](http://stackoverflow.com/a/6803651/433790) helpful. – ridgerunner Sep 11 '14 at 14:51

5 Answers5

23

C# does not have a special syntax for construction of regular expressions, like Perl, Ruby or JavaScript do. It instead uses a constructor that takes a string. However, strings have their own escaping mechanism, because you want to be able to put quotes inside the string. Thus, there are two levels of escaping.

So, in a regular expression, w means the letter "w", while \w means a word character. However, if you make a string "\w", you are escaping the character "w", which makes no sense, since character "w" is not a quote or a backslash, so "w" == "\w". Then this string containing only "w" gets passed to the regexp constructor, and you end up matching the letter "w" instead of any word character. Thus, to pass the backslash to regexp, you need to put in two backslashes in the string literal (\\w): one will be removed when the string literal is interpreted, one will be used by the regular expression.

When working with regular expressions directly (such as on most online regexp testers, or when using verbatim strings @"..."), you don't have to worry about the interpretation of string literals, and you always write just one backslash (except when you want to match the backslash itself, but then you're espacing the backslash for the regexp, not for the string).

asontu
  • 4,548
  • 1
  • 21
  • 29
Amadan
  • 191,408
  • 23
  • 240
  • 301
7

\ Is also an escape character for string literals in c# so the first \ is escaping the second \ being passed to the method and the second one is escaping the . in the regex.

Use:

if (Regex.IsMatch(myString, @"SomeString\."))

If you want to avoid double escaping.

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
5

I you use a verbatim symbol @(verbatim string), you don't need to escape the backslash again.

if (Regex.IsMatch(myString, @"SomeString\."))
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 1
    this is good but not really answering the question _Can someone explain to me when using regular expressions when a double backslash or single backslash needs to be used to escape a character?_ I didn't dv btw – Yuliam Chandra Sep 11 '14 at 09:19
  • I'm aware this is possible, but I'm looking for an explaination why it is required in my example and not in yours? – Duane Sep 11 '14 at 09:20
  • @Duane in java or c#, if you use only `"` as delimiters, you need to escape the backslash one more time..And also see Ben or Amadan answer for more info. – Avinash Raj Sep 11 '14 at 09:25
-1

Old post but Regex.Escape may be useful

NicolasV
  • 129
  • 2
  • 8
  • 1
    Not really. If you have the string "\b" and perform `Regex.Escape()` on it you will not get "\\b" like you seem to expect, because "\b" is _already_ a special character in the original string, and will never be seen as "\" + "b" in the compiled code. – Nyerguds Aug 10 '18 at 13:34
  • God damn it, this method sucks, it is especially bad when dealing with regex special chars like backslashes within a string. – Harvey Lin May 30 '20 at 23:25
-1

In JavaScript you have to use double escape character: \

let m = "My numer is [56]".match("\\[(.*)\\]"); 
alert(m[1]);//outputs 56

In C# single \

Robert Benyi
  • 1,623
  • 1
  • 14
  • 10