locked
problem with " \\ " RRS feed

  • Question

  • Hello.

    I have problem with converting path.

    I have something like this:

    Code Block

    string path = "C:\\something\\here\\file.exe";
    string newpath = Regex.Replace(path, "here\\file.exe", "nothere");
    MessageBox.Show(newpath);



    But when I run it, the path doesn't change.

    Friday, September 28, 2007 10:06 AM

Answers

  • Ok. But i try this

    Code Block

    string path = @"C:\something\here\file.exe";
    string newpath = Regex.Replace(path, "here\\file.exe", "nothere");
    MessageBox.Show(newpath);


    But it still doesn't replace my path...
    Friday, September 28, 2007 10:22 AM
  • try this:

    Code Block

    string path = @"C:\something\here\file.exe";
    string newpath = Regex.Replace(path, @"here\\file.exe", "nothere");
    MessageBox.Show(newpath);


    Friday, September 28, 2007 10:29 AM
  • As weenoid has alluded to, the backslash is a special character in the Replace method, which takes a regular expression, not just a simple literal string. If you want to literally look for a backslash, then it needs to be 'escaped' as a double-backslash. So either:

    Replace(path, "here\\\\file.exe", "nothere");

    or as weenoid said.

    Either way makes the resulting Regex expression be literally 'here\\file.exe', which is what is needed to escape the literal backslash with another backslash.
    Friday, September 28, 2007 3:34 PM

All replies

  •  Vasquez21 wrote:
    Hello.

    I have problem with converting path.

    I have something like this:

    Code Block

    string path = "C:\\something\\here\\file.exe";
    string newpath = Regex.Replace(path, "here\\file.exe", "nothere");
    MessageBox.Show(newpath);



    But when I run it, the path doesn't change.

    Use a verbatim string literal. I think its much easier than backslashes in strings:

     

    string path = @"C:\something\here\file.exe";
    string path = "C:\\something\\here\\file.exe";
    string newpath = Regex.Replace(path, "here\\file.exe", "nothere");
    MessageBox.Show(newpath);
    MessageBox.Show(newpath);

     

    As you can see just append an "at" sign to the declaration of the string.

     

     

    When you're saying the path doesn't change its because you're attempting to replace:


    "here\\file.exe" with "", the path is C:\something\here\file.exe not C:\\something\\here\\file.exe

     

    You could also use String.Replace rather than regex.

    Friday, September 28, 2007 10:14 AM
  • Ok. But i try this

    Code Block

    string path = @"C:\something\here\file.exe";
    string newpath = Regex.Replace(path, "here\\file.exe", "nothere");
    MessageBox.Show(newpath);


    But it still doesn't replace my path...
    Friday, September 28, 2007 10:22 AM
  • try this:

    Code Block

    string path = @"C:\something\here\file.exe";
    string newpath = Regex.Replace(path, @"here\\file.exe", "nothere");
    MessageBox.Show(newpath);


    Friday, September 28, 2007 10:29 AM
  • As weenoid has alluded to, the backslash is a special character in the Replace method, which takes a regular expression, not just a simple literal string. If you want to literally look for a backslash, then it needs to be 'escaped' as a double-backslash. So either:

    Replace(path, "here\\\\file.exe", "nothere");

    or as weenoid said.

    Either way makes the resulting Regex expression be literally 'here\\file.exe', which is what is needed to escape the literal backslash with another backslash.
    Friday, September 28, 2007 3:34 PM