Java Regex to capture multiple matches

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()); } 
+4
source share
5 answers

What you ask for is impossible; the find method works, each match can only match after the end of the previous match. However, you can write:

 final Pattern p = Pattern.compile("[^/]/"); final String s = "/accounts/main/index/page.xhtml"; final Matcher m = p.matcher(s); while (m.find()) { System.out.println(s.substring(0, m.end())); } 

Or to get an array:

 final Pattern p = Pattern.compile("[^/]/"); final String s = "/accounts/main/index/page.xhtml"; final Matcher m = p.matcher(s); final List<String> resultList = new ArrayList<String>(); while (m.find()) { resultList.add(s.substring(0, m.end())); } final String[] resultArr = resultList.toArray(new String[resultList.size()]); 

(Disclaimer: not verified.)

+2
source

I would not use a regular expression for this. What about something in the lines

 String[] split = s.split("/"); StringBuilder sb = new StringBuilder(s.lastIndexOf('/') + 1); // our result sb.append('/'); // initial "/" for (int i = 0; i < split.length - 1; i++) { // we don't care about the if (split[i].isEmpty()) // last component continue; sb.append(split[i]); sb.append('/'); System.out.println(sb); // or add to an array/list/etc. } 
  / accounts /
 / accounts / main /
 / accounts / main / index /
+3
source

Another way:

 Pattern p = Pattern.compile("/[^/]+"); String s = "/accounts/main/index/page.xhtml"; String dir = ""; Matcher m = p.matcher(args[0]); while(m.find()){ dir += m.group(); System.out.println(dir + "/"); } 
+1
source

In fact, this can be done using regular expressions that will work for your example:

 Pattern p = Pattern.compile("^(((/[^/]+/)[^/]+/)[^/]+/)"); String s = "/accounts/main/index/page.xhtml"; Matcher m = p.matcher(s); while (m.find()) { System.out.println(m.group(1)); System.out.println(m.group(2)); System.out.println(m.group(3)); } 

however, you cannot have a regular expression matching each individual case. But since the structure of the regular expression is well defined, you can, of course, build it on the fly depending on how deep your directory structure is, and then compile it every time.

0
source

Regex is ok to split, but you need to add code:

 String parts = a.split("(?<!^)(?=/)"); for (int i = 0; i < parts.length - 2; i++) parts[i + 1] = parts[i] + parts[i + 1]; 
0
source

Source: https://habr.com/ru/post/1493318/


All Articles