I would like to have a regular expression to extract the path from a full file location
For example, having a path
#/home/java/bin/myfile
The regular expression should extract /home/java/bin/
I would like to have a regular expression to extract the path from a full file location
For example, having a path
#/home/java/bin/myfile
The regular expression should extract /home/java/bin/
Why regular expression?
String s = "#/home/java/bin/myfile";
String result = s.substring(1, s.lastIndexOf("/") + 1);
The most simple regex pattern would be .*\/(?!.*\/)|
You can extract the directory from the path using this line:
String dir = path.replaceAll(".*?(/.*/).*", "$1");
Here's some test code:
String path = "#/home/java/bin/myfile";
String dir = path.replaceAll(".*?(/.*/).*", "$1");
System.out.println(dir);
Output:
/home/java/bin/
replaceAll("^[^/]*|[^/]*$", "");