给定一个字符串,可能包含一个或多个由“;”分隔的 SQL 语句,例如:
String sql = "select * from table1 where col1 = 'abc;de'; select * from table2;";
我需要在字符串数组中获取语句:
array[0] = "select * from table1 where col1 = 'abc;de';"
array[1] = "select * from table2;"
请注意,撇号之间可以出现分号。
例如,使用正则表达式:
String regex = "???"; // <--- I can't figure out this one
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(sql);
实现此目的的正则表达式是什么?
最佳答案
您可以尝试以下正则表达式:
\s*;\s*(?=([^']*'[^']*')*[^']*$)
示例如下:
public static void main(String[] args) {
String input = "select * from table1 where col1 = 'abc;de'; select * from table2;";
System.out.println(java.util.Arrays.toString(
input.split("\\s*;\\s*(?=([^']*'[^']*')*[^']*$)")
)); // prints "[select * from table1 where col1 = 'abc;de', select * from table2]"
}
正则表达式说明:
NODE EXPLANATION
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
; ';'
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
( group and capture to \1 (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
[^']* any character except: ''' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
' '\''
--------------------------------------------------------------------------------
[^']* any character except: ''' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
' '\''
--------------------------------------------------------------------------------
)* end of \1 (NOTE: because you are using a
quantifier on this capture, only the
LAST repetition of the captured pattern
will be stored in \1)
--------------------------------------------------------------------------------
[^']* any character except: ''' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
关于java - Java中使用正则表达式分隔多个SQL语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28843190/