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)
.replaceAllor.replaceFirstinstead of.replace?.replacedoes not take a regex. If you want to replace a single occurrence, then use.replaceFirst