Well, actually there are several, but it depends on what exactly you want to analyze. Do you want something like: "C: /.../.../ nameOfFile.extension"? Or without a file name? Can you clarify the problem? Which input?
I made this code really simple, which for this path gives you a path without root.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestProgram {
static String path = "C:\\Folder1\\Folder2\\file.extension";
private static void parsePath() {
String newPath = "";
String root = "C:";
Matcher regexMatcher;
regexMatcher = Pattern.compile("\\\\").matcher(path);
newPath = regexMatcher.replaceAll("/");
regexMatcher = Pattern.compile(root).matcher(newPath);
newPath = regexMatcher.replaceAll("");
System.out.println(newPath);
}
public static void main(String[] args) {
parsePath();
}
}
Conclusion:
/Folder1/Folder2/file.extension
I donβt know if you want this, but you just need to play using the methods. You will eventually reach a solution.
source
share