【问题标题】:Regex for SQL insert query用于 SQL 插入查询的正则表达式
【发布时间】:2015-06-08 17:39:19
【问题描述】:

我正在尝试获取 SQL 插入查询的正则表达式。我需要从查询中提取表名。这应该是什么正则表达式?我正在使用 Java

我查看了此链接,但无法获取表名。 Regular expression to extract SQL query

【问题讨论】:

    标签: java regex


    【解决方案1】:

    正则表达式不是从 SQL 查询中提取表名的完全证明解决方案...由于必须考虑大量的事情,这在 RegX 中表达起来会更棘手,并且会在一种或其他情况下爆发... .

    您可以使用以下超轻、超快速的库以优雅的方式完成此操作

    <dependency>
      <groupId>com.github.mnadeem</groupId>
      <artifactId>sql-table-name-parser</artifactId>
      <version>0.0.1</version>
    

    并执行以下查询

     new TableNameParser(sql).tables()
    

    更多详情请参考project

    免责声明:我是图书馆的所有者

    【讨论】:

      【解决方案2】:

      对于那种类型的查询

      INSERT INTO 
          table_name 
        VALUES 
          (value1,'value which contains semicolon ;;;;',value3,...);
      
      public static String regex = "(?i)insert into//s+(//S+)//s+"
      
          public static void printMatches(String text, String regex) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(text);
            // Check all occurrences
            while (matcher.find()) {
              System.out.println(" Found: " + matcher.group().replaceAll("(?i)insert into", "");
            }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-20
        • 1970-01-01
        • 2021-12-28
        • 2023-04-06
        • 2013-05-16
        相关资源
        最近更新 更多