Overlapping regular expressions part II
In this question I asked for a regex to turn /abc/def/ghi
into {/abc/def/ghi
, /def/ghi
, /ghi
}. The answer was to use lookahead with 开发者_JAVA百科the expression (?=(/.*))
.
Is there a regex to capture from the same string {/abc
, /abc/def
, /abc/def/ghi
}? (Order is not important.)
Ok, here's a solution that works for your one and only test case, though I haven't found a way to group it into one nice group:
Matcher m = Pattern.compile("((?<=(^.*))(/[^/]*))").matcher("/abc/def/ghi");
while (m.find()) {
System.out.println(m.group(2) + m.group(3));
}
It essentially finds each /xxx substring as they appear but then also concatenates everything before that match. This works for your test case but might have limitations for more complex cases.
This will do what you want:
(?<=(/.*)\b)
精彩评论