0

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

After this variable uses in:

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

But this line contain an error, because wordSearch in this case is "T:\", and the program trhow me an exception like that:

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

Thanks!

Ou! I have a solution! Is this!

StreamWriter writer = new StreamWriter(filePath, false, Encoding.Default);
writer.Write(content.Replace(wordSearch, wordReplace));
writer.Close();
Mr.Beto
  • 115
  • 1
  • 4
  • 12
  • use a verbatim @ , \ is a special case rather than having \\ which will work use String str = @"yourStringValue"; – bumbumpaw Jul 10 '14 at 07:21
  • If I put this : content = Regex.Replace(content, @wordSearch, @wordReplace); The error is the same :S – Mr.Beto Jul 10 '14 at 07:25
  • And then I put string str = @wordSearch; string str1 = @wordReplace; content = Regex.Replace(content, str, str1); And the error persist... – Mr.Beto Jul 10 '14 at 07:29

1 Answers1

1

You need to escape it twice since it is both a regex and C# escape character. "T:\\\\"

etr
  • 1,252
  • 2
  • 8
  • 15