Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用正则表达式将字符串拆分为几个子字符串。应同时返回匹配项和非匹配项_Java_Regex - Fatal编程技术网

Java 使用正则表达式将字符串拆分为几个子字符串。应同时返回匹配项和非匹配项

Java 使用正则表达式将字符串拆分为几个子字符串。应同时返回匹配项和非匹配项,java,regex,Java,Regex,我想把一个字符串拆分成几个子字符串,我认为使用正则表达式可以帮助我 I want this: To become this: <choice1> {<choice1>} c<hoi>ce2 {c, <hoi>, ce2} <ch><oi><ce>3 {<ch>, <oi>, <ce>, 3} choice4

我想把一个字符串拆分成几个子字符串,我认为使用正则表达式可以帮助我

I want this:        To become this:
<choice1>           {<choice1>}
c<hoi>ce2           {c, <hoi>, ce2}
<ch><oi><ce>3       {<ch>, <oi>, <ce>, 3}
choice4             {choice4}
我希望这个:变成这个:
{}
cce2{c,ce2}
3       {, , 3}
选择4{choice4}
请注意,花括号和逗号只是视觉辅助工具。最终形式是什么并不重要,只是这些值可以单独访问/重放

提前感谢。

带split

input.split("(?<!^)(?=<)|(?<=>)(?!$)");
input.split(“(?)(?!$”);

虽然我会匹配他们

Matcher m=Pattern.compile("<[^>]*>|[^<>]+").matcher(input);
while(m.find())
{
     m.group();//matched value
}
Matcher m=Pattern.compile(“]*>|[^]+”).Matcher(输入);
while(m.find())
{
m、 group();//匹配的值
}
此代码应适用于:

String str = "<ch><oi><ce>3";
Pattern p = Pattern.compile("<[^>]*>|\\w+");
Matcher m = p.matcher(str);
while(m.find())
    System.out.printf("=> %s%n", m.group());
String str=“3”;
模式p=Pattern.compile(“]*>\\w+”;
匹配器m=p.Matcher(str);
while(m.find())
System.out.printf(“=>%s%n”,m.group());
输出:

=> <ch>
=> <oi>
=> <ce>
=> 3
=>
=> 
=> 
=> 3

字符串有一个方法split,它将返回和正则表达式匹配的部分数组。“asfdasdfadsf”。拆分(“正则表达式”);给出了这些例子,我认为这更容易理解:| \\w+,但你的答案也是正确的。+1不要介意蹩脚的Matcher版本。你的分裂正则摇滚!