Java - 由分隔符拆分

500*_*865 2 java regex split

鉴于_<A_>_<B_>_<Z_>,我想提取A, B, C一个数组.

基本上_<是起始分隔符,_>是结束分隔符.

ste*_*ema 5

您可以使用环绕声断言来仅匹配标记的内容.

String text = "_<A_>_<B_>_<Z_>";

List<String> Result = new ArrayList<String>();

Pattern p = Pattern
    .compile("(?<=_<)" + // Lookbehind assertion to ensure the opening tag before
        ".*?" +          // Match a less as possible till the lookahead is true 
        "(?=_>)"         // Lookahead assertion to ensure the closing tag ahead
        );
Matcher m = p.matcher(text);
while(m.find()){
    Result.add(m.group(0));
}
Run Code Online (Sandbox Code Playgroud)