Edited: Added relative path processing
String path; String root = path.replaceAll( "((?<=/)|((?<=^)(?=\\w)))(?!(\\w|\\\\\\.)+/.*).*", "" ) );
Here's the test:
public static void main( String[] args ) throws IOException { String[] paths = {"/abc/def*/bc", "/abc/def*", "/ab.c/def*", "/ab\\.c/def*", "abc*", "abc/bc"}; for ( String path : paths ) { System.out.println( path + " --> " + path.replaceAll( "((?<=/)|((?<=^)(?=\\w)))(?!(\\w|\\\\\\.)+/.*).*", "" ) ); } }
Conclusion:
/abc/def*/bc --> /abc/ /abc/def* --> /abc/ /ab.c/def* --> / /ab\.c/def* --> /ab\.c/ abc* --> abc/bc --> abc/
You can configure it from there.
source share