对不起,如果问题是混乱的。从下面的文字
value = ( select max( tbl.column_name ) from table tbl where trim(colum2)='value1'
and (trim((colum3)))='value3')
我想要下面的输出
select max( tbl.column_name ) from table tbl where trim(colum2)='value1'
and (trim((colum3)))='value3'
基本上从 select max( until ) except ) 开始匹配左括号。
(select[ ]*max[ ]*\(.*column_name[ ]*\)[^)]*)
这只匹配到 trim(colum2 Need help to escape bracket ) 是否有一个左括号,包括任何嵌套
谢谢
编辑:我终于做到了java,如下所示。但想知道REGEX解决方案
String sql = readFile(file.getPath());
Pattern patTabs = Pattern.compile("(\\([ \n]*SELECT[ \n]*MAX[ \n]*\\(.*COLUMN_NAME[ \n]*\\))", Pattern.CASE_INSENSITIVE);
Matcher tabMat = patTabs.matcher(sql);
while (tabMat.find()) {
int i = tabMat.start();
int j = tabMat.end();
int l = 0;
for (j = tabMat.end(); j < sql.length(); j++) {
char k = sql.charAt(j);
if (k == '(') {
l++;
}
if (k == ')') {
if (l == 0) {
break;
} else {
l--;
}
}
}
System.out.println(sql.substring(i, j + 1))
}