-1

Trying to strip server name from: //some.server.name/path/to/a/dir (finishing with /path/to/a/dir)

I have tried 3 different regexes (hardcoded works), but the other two look like they should work but are not. Can anyone point me to why ?

cat test.java

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class test
{
public static void main(String[] args) throws Exception
{

        String rootPath="//server.myco.com/some/path/to/a/doc/root";
        rootPath = rootPath.replace("//[\\w.]*","");
        System.out.println("rootPath - "+rootPath);
        rootPath = rootPath.replace("//[^/]*","");
        System.out.println("rootPath - "+rootPath);
        rootPath = rootPath.replace("//server.myco.com","");
        System.out.println("rootPath - "+rootPath);

}
}

Output:

rootPath - //server.myco.com/some/path/to/a/doc/root
rootPath - //server.myco.com/some/path/to/a/doc/root
rootPath - /some/path/to/a/doc/root

Java 11.0.6:

$ java --version
openjdk 11.0.6 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.6+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.6+10, mixed mode)
3
  • 2
    Were you looking for .replaceAll or .replaceFirst instead of .replace ? Commented Nov 14, 2023 at 18:25
  • Thanks, replaceAll did it. Why did I need replaceAll ? There is a single instance in the string. Commented Nov 14, 2023 at 18:28
  • Using .replace does not take a regex. If you want to replace a single occurrence, then use .replaceFirst Commented Nov 14, 2023 at 18:34

3 Answers 3

3

Please refrain from using a regular expression to parse a URI. Most languages these days include libraries to parse a URI/URL into a various parts.

Here is a proper example:

import java.net.URI;
import java.net.URISyntaxException;

public class ExtractPath {
    public static void main(String[] args) {
        String rootPath = "//server.myco.com/some/path/to/a/doc/root";
        String path = extractPath(rootPath);
        System.out.println(path.equals("/some/path/to/a/doc/root")); // true
    }

    public static String extractPath(String location) {
        try {
            return new URI(location).getPath();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Do not use URL for checking/parsing, use URI instead: "As mentioned, URL and URLConnection rely on protocol handlers which must be present, otherwise an Exception is thrown."
Its not a URL per se, but I see your point. Thanks.
@UsagiMiyamoto I addressed this. Thanks.
When you’re going to wrap the URISyntaxException in an unchecked exception anyway, you can use return URI.create(location).getPath(); in the first place. Then, you’ll get an IllegalArgumentException wrapping the URISyntaxException if one occurred, without the need to do it yourself.
1

You can utilize the URI class for this.

URI u = new URI("//server.myco.com/some/path/to/a/doc/root");
String p = u.getPath();

Output

/some/path/to/a/doc/root

Comments

0

You should use this from the String class:

public String replaceAll(String regex, String replacement) 

instead of this:

public String replace(CharSequence target, CharSequence replacement)

When you are calling replace("//[\\w.]*","") or replace("//[^/]*",""), it's searching "//[\\w.]*" or "//[^/]*" respectively, and trying to replace with the empty string. As there is no such string in the given string, no change is there.

But replaceAll("//[\\w.]*","") takes regex as the first argument. Hence it will work.

Oracle Java 11 reference for String replace() and replaceAll()

Comments

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.