【发布时间】:2019-10-04 20:01:57
【问题描述】:
我想提取方括号内的字符串内容(如果一个方括号内包含嵌套的方括号,应该忽略)。
例子:
c[ts[0],99:99,99:99] + 5 - d[ts[1],99:99,99:99, ts[2]] + 5
应该返回:
match1 = "ts[0],99:99,99:99";
match2 = "ts[1],99:99,99:99, ts[2]";
到目前为止,我的代码仅适用于非嵌套方括号
String in = "c[ts[0],99:99,99:99] + 5 - d[ts[1],99:99,99:99, ts[2]] + 5";
Pattern p = Pattern.compile("\\[(.*?)\\]");
Matcher m = p.matcher(in);
while(m.find()) {
System.out.println(m.group(1));
}
// print: ts[0, ts[1, 2
【问题讨论】:
-
当有嵌套括号但在
Should return中有外部括号的嵌套括号时应该忽略?可以加一个不应该返回的例子吗?
标签: java regex algorithm parsing