java - Java中使用正则表达式分隔多个SQL语句

标签 java regex

给定一个字符串,可能包含一个或多个由“;”分隔的 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*(?=([^']*'[^']*')*[^']*$)

Visual Regex

示例如下:

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/

相关文章:

python - 如何使用正则表达式提取文档/文本中的所有引号?

正则表达式 最后一次出现?

r - 在 R 的正则表达式中是否有等效的 "&"用于反向引用整个匹配?

python - 正则表达式和组的交集

java - Java SAX 解析器是否真的从互联网上检索 DTD?

c# - .Net Regex - 最后一个重复字符

java - JSTL 迭代列表

java - 在 Spring MVC 4 中将 POJO 作为 JSON 返回

java - Mysql 需要很长时间才能检索结果

java - 执行基本 if else 语句时出现编译错误