给定一个字符串,其中可能包含一个或多个由";"分隔的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);
要完成此任务的正则表达式是什么?
答案 0 :(得分:9)
您可以尝试以下正则表达式:
\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
答案 1 :(得分:0)
我在战斗后过了一会儿,但是如果这可以帮助某人...
我发现的最简单的方法是依靠IBatis migration tool,它具有一个批处理文件处理工具,该工具可在内部将文件拆分为语句。
在内部,它们在ScriptRunner类中使用:
private void CaptureCamera()
{
camera = new Thread(new ThreadStart(CaptureCameraCallback));
camera.Start();
}
private void CaptureCameraCallback()
{
frame = new Mat();
capture = new VideoCapture();
capture.Open(2);
while (isCameraRunning == 1)
{
capture.Read(frame);
image = BitmapConverter.ToBitmap(frame);
pictureBox1.Image = image;
image = null;
}
}
public frmFaceDetection()
{
InitializeComponent();
}