1

I have a path called $SERVER/public_html/ab1/ab2/.

I want to change it so that instead of $SERVER it just replaces it with my user directory. So I do

path = path.replaceFirst("\\$SERVER", System.getProperty("user.dir"));

but when I run it, it removes my \ in the new string.

F:Programming ProjectsJava Project/public_html/ab1/ab2/
1
  • You can just do replace('\\','/') Commented May 20, 2011 at 23:56

3 Answers 3

3

Pattern has a String quote(String) function that will help you for the first string and Matcher has String quoteReplacement(String) for the second:

path = path.replaceFirst(java.util.regex.Pattern.quote("$SERVER"), java.util.regex.Matcher.quoteReplacement(System.getProperty("user.dir")));

edit: the reason you have to escape anything is because the second string has the semantics of Matcher.appendReplacement which treats backslashes and dollars as escape next char and insert captured group resp.

from the doc:

Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

a more obvious solution is (be careful of the needed escaped with that backslash)

 path = path.replaceFirst("\\$SERVER", System.getProperty("user.dir").replaceAll("\\\\","\\\\\\\\"));
Sign up to request clarification or add additional context in comments.

3 Comments

Very interesting solution. I like it. +1
Thanks this work but replaceFirst is basic calling Pattern.compile(regex).matcher(str).replaceFirst(repl)
+1 - This is the correct explanation and the correct solution.
0

Yea you are completly right. I am trying to figure out why it is happening so.

But at the moment the only think I can suggest is to go with such a solution.

public class RegExTest
{
    public static void main(String[] args)
    {
        String path = "$SERVER/public_html/ab1/ab2";
        System.out.println("path before="+path);
        String user = System.getProperty("user.dir");       
        System.out.println("user="+user);
        System.out.println("replaceFirst using user="+path.replaceFirst("\\$SERVER", user));
        path = path.replaceFirst("\\$SERVER", "");
        path = user +path;
        System.out.println("path after="+path);
    }
}

EDIT: ..Why it does that?

From what I see in the code of the method line 701 to 708 they must do it. They just skip them. As to the reason why they do it, I still am not sure.

EDIT2: OK reading the doc for the method answers it all. They do it so they can interpret accordingly special characters. Thus when reading the replacement they spot a slash the algorithm assumes it can be a part of special character and in result skips it.

            if (nextChar == '\\') {
                cursor++;
                nextChar = replacement.charAt(cursor);
                result.append(nextChar);
                cursor++;
            } else if (nextChar == '$') {
                // Skip past $
                cursor++;

2 Comments

Thanks I figured out another way to do it, I'm just little curious on why it's doing that in the first place.
@K.T check the edit. There is a code snippet from the method which I would say is responsible for it.
0

Ok so in Windows the default slashes look like so '\' whereas on *nix the slashes look like so '/' . The simplest way to get through this problem is to invoke the replace function with the following parameters '\\' and '/' . That way you path will have its slashes all facing the same way.

2 Comments

Yes ... this will work, but it is not a general solution. Suppose for instance that he didn't want to change \ to / ... or that he wanted to change / to \. FWIW, the File(String) constructor is happy with mixed \ and / in raw pathnames ... it normalizes to the default file separator.
Some command line functions will fail if the slash is wrong. The general solution I've seen is to check the operating system and swap as needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.