开发者

Overlapping regular expressions part II

开发者 https://www.devze.com 2023-04-13 02:34 出处:网络
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 (?=(/.*)).

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消