我的正则表达式无法捕获正确的括号组。在我的方法中,我有:
Pattern pattern = Pattern.compile("\\s*union\\s+(\\(.+\\)|\\S+)\\s+and\\s+(\\(.+\\)|\\S+)\\s*;\\s*", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if(matcher.matches())
{
group1 = matcher.group(1);
group2 = matcher.group(2);
}
我想要捕获外括号内的单词或内容。在简单的情况下,这可以正常工作(即,如果输入为union a or b;
或union (union a and b) and c;
或甚至union (intersect table1 and table2) and (minus table4 and (union table1 and table2));
)
但如果输入是:
union (union a and b) and (union (union a and c) and d);
我明白了:
group1 = union a and b) and (union (union a and c
group2 = table1)
但我希望group1和group2成为:
group1 = union a and b
group2 = union (union a and c) and d
<
>在类似的输入中,它似乎是从最后的&#39;和#39;中捕获的。另一个例子:
input = union (union a and b) and (union (union c and d) and (union e and f));
group1 = (union a and b) and (union (union c and d)
group2 = (union e and f))
如何捕获正确的括号组?
答案 0 :(得分:0)
您无法使用扫描仪进行解析。你需要一个解析器。正则表达式无法处理嵌套括号。他们是完成这项任务的错误技术。