2

Well I have a mini program with C#, so I do this in my program

wordSearch = "T:\\"
wordReplace = "T:\\Gestion\\"
content = Regex.Replace(content, wordSearch, wordReplace);

But doesn't work. The error is:

parsing "T:\" - illegal \ at end of pattern.

Any idea ?

[Added]

Sorry! Perhaps I didn't explain well. So I try again.

I did a form taking a string for input, but if this string is "T:\", the program take "T:\". So, I save this string in variable "workShearch".

After this variable uses in:

content = Regex.Replace(content, Regex.Escape(wordSearch), Regex.Escape(wordReplace));

But this line contains an error, because wordSearch in this case is "T:\", and the program throws an exception like this:

The error in parsing "T:\" - illegal \ at end of pattern.

Thanks!

Rezaeimh7
  • 1,467
  • 2
  • 23
  • 40
Mr.Beto
  • 115
  • 1
  • 4
  • 12
  • 1
    Correct me if I'm wrong, but \ is an escape sequence, that's the problem. Try this: `content = Regex.Replace(content, @wordSearch, @wordReplace);` – DatRid Jul 09 '14 at 07:36
  • 1
    Yes, it's an escape sequence both in Strings and Regexes, so the correct Regex is either `"T:\\\\Gestion\\\\"` or `@"T:\Gestion\"` – Davio Jul 09 '14 at 07:37
  • It's both an regex and a C# string escape character, which means it would have to be escaped twice, i.e. "T:\\\\". – Dirk Jul 09 '14 at 07:37
  • The answers below are fine... but why are you using regular expressions for this? A simple string replace would suffice in this case. i.e. `content = String.Replace(wordSearch,wordReplace);` – Jcl Jul 09 '14 at 07:38
  • 1
    @DatRid, Correct me if I'm wrong, but I don't think you can use the @ on variables, just on strings. The @ on variables is to distinguish them from keywords, they don't make a string variable verbatim. – Jcl Jul 09 '14 at 07:41
  • @Jcl Correct me if I'm wrong, but I think you are right! My bad. – DatRid Jul 09 '14 at 07:57
  • @DatRid I was really not trying to mock on the "correct me if I'm wrong" part, just didn't see you wrote the same! :-) – Jcl Jul 09 '14 at 08:02
  • @Jcl No problem, I just found it funny! :) – DatRid Jul 09 '14 at 08:05

3 Answers3

7

You should escape \ in pattern. Either use "T:\\\\" or verbatim string literal ( advantage of verbatim strings is that escape sequences are not processed, which makes it easy to write):

var wordSearch = @"T:\\";
var wordReplace = @"T:\Gestion\";

content = Regex.Replace(content, wordSearch, wordReplace);
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
2

Escape your '\' again.

wordSearch = "T:\\\\"

A more elegant solution would be to use the @ modifier infront of your strings

wordSearch = @"T:\\"
Chrisi
  • 371
  • 3
  • 15
2

\ is escape character, if you want to have \\, you should type \\\\ or place @ in front of your string, which will consider whole string is a plain text

wordSearch = @"T:\\"
wordReplace = @"T:\\Gestion\\"

// or 

wordSearch = "T:\\\\"
wordReplace = "T:\\\\Gestion\\\\"
Prisoner
  • 1,839
  • 2
  • 22
  • 38