Java 用括号具体拆分字符串
我希望按以下方式拆分字符串:Java 用括号具体拆分字符串,java,regex,string,split,Java,Regex,String,Split,我希望按以下方式拆分字符串: String s = "dotimes [sum 1 2] [dotimes [sum 1 2] [sum 1 3]]" 结果: {"dotimes", "[sum 1 2]", "[dotimes [sum 1 2] [sum 1 3]]" 我尝试使用这个正则表达式: s.split("\\s(?=\\[)|(?<=\\])\\s") 是否有任何方法可以使用正则表达式以我想要的方式拆分字符串 是否有任何方法可以使用正则表达式以我想要的方式拆分字符串
String s = "dotimes [sum 1 2] [dotimes [sum 1 2] [sum 1 3]]"
结果:
{"dotimes", "[sum 1 2]", "[dotimes [sum 1 2] [sum 1 3]]"
我尝试使用这个正则表达式:
s.split("\\s(?=\\[)|(?<=\\])\\s")
是否有任何方法可以使用正则表达式以我想要的方式拆分字符串
是否有任何方法可以使用正则表达式以我想要的方式拆分字符串
不,没有。正则表达式(如果匹配)返回由()
包围的字符串和子字符串,或者如果使用全局标志,返回所有完整匹配的列表。您不会得到其他匹配项的子项的嵌套列表
将其与Java结合就可以了。我不懂Java,但我会尝试用类似Java的代码来解释:
Array match_children (Array input) {
Array output;
foreach (match in input) {
// The most important part!
// The string starts with "[", so it is the beginning of a new nest
if (match.startsWith("[")) {
// Use the same ragex as below
Array parents = string.match(matches 'dotimes' and all between '[' and ']');
// Now, call this same function again with the
match = match_children(parents);
// This stores an array in `match`
}
// Store match in output list
output.push(match);
}
return output;
}
String string = "dotimes [sum 1 2] [dotimes [sum 1 2] [sum 1 3]]";
// "dotimes [sum 1 2] [dotimes [sum 1 2] [sum 1 3]]"
Array parents = string.match(matches 'dotimes' and all between '[' and ']');
// "dotimes", "[sum 1 2]", "[dotimes [sum 1 2] [sum 1 3]]"
// Make sure to use a global flag
Array result = match_children(Array input);
// dotimes
// [
// sum 1 2
// ]
// [
// dotimes
// [
// sum 1 2
// ]
// [
// sum 1 3
// ]
// ]
再说一遍,我不懂Java,如果它需要更清晰的说明,只需注释即可
希望这能有所帮助。这很管用,虽然不是特别漂亮,但如果没有OP中的正式语法,概括起来可能会很差
{
//String s = "sum 1 2";
String s = "dotimes [sum 1 2] [dotimes [sum 1 2] [sum 1 3]]";
int depth = 0;
int pos = 0;
for (int c = 0; c <= s.length(); ++c){
switch (c == s.length() ? ' ' : s.charAt(c)){
case '[':
if (++depth == 1){
pos = c;
}
break;
case ' ':
if (depth == 0){
String token = s.substring(pos, c == s.length() ? c : c + 1);
if (!token.matches("\\s*")){ /*ingore white space*/
System.out.println(token);
}
pos = c + 1;
}
break;
case ']':
if (--depth == 0){
String token = s.substring(pos, c + 1);
if (!token.matches("\\s*")){ /*ingore white space*/
System.out.println(token);
}
pos = c + 1;
}
break;
}
}
}
{
//字符串s=“sum 1 2”;
字符串s=“dotimes[sum 12][dotimes[sum 12][sum 13]”;
int深度=0;
int pos=0;
对于(int c=0;c括号可以任意嵌套吗?检查。方法是在空格上拆分,然后是平衡的开始括号和结束括号。@RohitJain:不幸的是,这个技巧在这里不起作用,因为OP有嵌套的方括号。@anubhava哦!对了,错过了那个。@Bec也许可以举一个例子说明正则表达式输出/匹配应该是什么,是吗我不清楚你期望的是什么谢谢芭丝谢芭!字符串“sum 12”返回的只是一个带有“sum”的列表而不是{“sum”,“1”,“2”}有什么原因吗?@Bec:是的;我就是这样解释这个问题的。[sum 12]不会把它分开。对不起,我的意思是1和2似乎在代码中丢失了?如果我在“sum 12”上运行代码它只返回一个包含“sum”的列表。我已经修改了。不过现在有点乱了。
{
//String s = "sum 1 2";
String s = "dotimes [sum 1 2] [dotimes [sum 1 2] [sum 1 3]]";
int depth = 0;
int pos = 0;
for (int c = 0; c <= s.length(); ++c){
switch (c == s.length() ? ' ' : s.charAt(c)){
case '[':
if (++depth == 1){
pos = c;
}
break;
case ' ':
if (depth == 0){
String token = s.substring(pos, c == s.length() ? c : c + 1);
if (!token.matches("\\s*")){ /*ingore white space*/
System.out.println(token);
}
pos = c + 1;
}
break;
case ']':
if (--depth == 0){
String token = s.substring(pos, c + 1);
if (!token.matches("\\s*")){ /*ingore white space*/
System.out.println(token);
}
pos = c + 1;
}
break;
}
}
}