用于匹配文本后跟文本直到闭括号的正则表达式,匹配左括号除外
Regex for matching text followed by text until closed bracket except matching opening bracket
抱歉,如果问题令人困惑。
来自下面的文字
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( till ) except ) 匹配左括号。
(select[ ]*max[ ]*\(.*column_name[ ]*\)[^)]*)
这只匹配到 trim(colum2
需要帮助转义括号 ) 是否有包含任何嵌套
的左括号
谢谢
编辑:
我终于做到了 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))
}
如果您想要一个优雅的解决方案,则必须将这些表达式中的行终止符替换为空格。所以你需要改变这个-
value = ( select max( tbl.column_name ) from table tbl where trim(colum2)='value1'
and (trim((colum3)))='value3')
到这个-
value = ( select max( tbl.column_name ) from table tbl where trim(colum2)='value1' and (trim((colum3)))='value3')
已编辑:之后,您可以使用\( *?(select *?max\( .*?column_name *?\).+) *?\)
来匹配您期望的表达式。
这是一个live demo
如果您真的不想替换表达式中的行终止符,而是想使用
value = ( select max( tbl.column_name ) from table tbl where trim(colum2)='value1'
and (trim((colum3)))='value3')
您可以使用这个\([ \n]*?(select[ \n]*?max\([ \n].*?column_name[ \n]\)[\D\d \n]+)[ \n]*?\)
。但我强烈建议不要这样做,因为如果您将多个 value = ( select..... )
提供给同一个模式,它将不起作用。
这是一个live demo
抱歉,如果问题令人困惑。 来自下面的文字
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( till ) except ) 匹配左括号。
(select[ ]*max[ ]*\(.*column_name[ ]*\)[^)]*)
这只匹配到 trim(colum2 需要帮助转义括号 ) 是否有包含任何嵌套
的左括号谢谢
编辑: 我终于做到了 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))
}
如果您想要一个优雅的解决方案,则必须将这些表达式中的行终止符替换为空格。所以你需要改变这个-
value = ( select max( tbl.column_name ) from table tbl where trim(colum2)='value1'
and (trim((colum3)))='value3')
到这个-
value = ( select max( tbl.column_name ) from table tbl where trim(colum2)='value1' and (trim((colum3)))='value3')
已编辑:之后,您可以使用\( *?(select *?max\( .*?column_name *?\).+) *?\)
来匹配您期望的表达式。
这是一个live demo
如果您真的不想替换表达式中的行终止符,而是想使用
value = ( select max( tbl.column_name ) from table tbl where trim(colum2)='value1'
and (trim((colum3)))='value3')
您可以使用这个\([ \n]*?(select[ \n]*?max\([ \n].*?column_name[ \n]\)[\D\d \n]+)[ \n]*?\)
。但我强烈建议不要这样做,因为如果您将多个 value = ( select..... )
提供给同一个模式,它将不起作用。
这是一个live demo