I have the following line that I want to use java regex to get the following results.
String s = "/accounts/main/index/page.txt" String[] result = {"/accounts/", "/accounts/main/", "/accounts/main/index/"};
That is, I would like to get a "parent directory hierarchy" (this should not be a directory structure).
NOTE. The string "s" is dynamically assigned, so these can be different levels of the directory.
I have the following, but I'm not sure how to compile a regex that will return me what I want. I know what I want, can only return one result, the last entry in the array:
Pattern p = Pattern.compile("^/.+/"); //how do i set up this regex to give me required results. String s = "/accounts/main/index/page.xhtml"; Matcher m = p.matcher(s); while(m.find()){ System.out.println(m.group()); }
source share