1

我想从下面名为“查询”的文件中查找表名和列名。

var query = "  SELECT accounts.name, SUM((COALESCE((jan_val_c),0)+  ";
query += "  COALESCE((feb_val_c),0)+ COALESCE((march_val_c),0)+ COALESCE((apr_val_c),0)+ ";
query += "  COALESCE((may_val_c),0)+ COALESCE((june_val_c),0)+ COALESCE((july_val_c),0)+   ";
query += "  COALESCE((aug_val_c),0)+ COALESCE((sept_val_c),0)+ COALESCE((oct_val_c),0)+   ";
query += "  COALESCE((nov_val_c),0)+ COALESCE((dec_val_c),0))) AS sales_plan,SUM((COALESCE((jan_actual_val_c),0)+   ";
query += "  COALESCE( (feb_actual_val_c),0)+ COALESCE( (march_actual_val_c),0)+ COALESCE( (apr_actual_val_c),0)+   ";
query += "  COALESCE( (may_actual_val_c),0)+ COALESCE( (june_actual_val_c),0)+ COALESCE( (july_actual_val_c),0)+   ";
query += "  COALESCE( (aug_actual_val_c),0)+ COALESCE( (sept_actual_val_c),0)+ COALESCE( (oct_actual_val_c),0)+   ";
query += "  COALESCE( (nov_actual_val_c),0)+ COALESCE( (dec_actual_val_c),0))) AS Actual_plan ,month_name_c,  ";
query += "   cl_sales_planning_month.year_c, cl_products.volume,cl_brands.name AS brand ,cl_therapies.name   ";
query += "   AS therapy,cl_products.name AS product, accounts.created_by,accounts.assigned_user_id ,   ";
query += "   DATE_FORMAT(STR_TO_DATE(CONCAT_WS('-',cl_sales_planning_month.month_name_c,  ";
query += "   cl_sales_planning_month.year_c),'%M-%Y'),'%b-%y' ) AS monthyear FROM cl_sales_planning_month   ";
query += "   LEFT JOIN accounts ON cl_sales_planning_month.account_id_c =accounts.id LEFT JOIN cl_products   ";
query += "   ON cl_sales_planning_month.cl_products_id_c = cl_products.id LEFT JOIN cl_brands ON   ";
query += "   cl_products.cl_brands_id_c=cl_brands.id LEFT JOIN cl_therapies ON   ";
query += "   cl_products.cl_therapies_id_c=cl_therapies.id WHERE   ";
 query += "            cl_sales_planning_month.month_name_c = MONTHNAME(CURRENT_DATE - INTERVAL 2 MONTH) AND  ";
      query += "            cl_sales_planning_month.year_c = YEAR(CURRENT_DATE - INTERVAL 2 MONTH)  AND";

query += "   cl_sales_planning_month.user_id_c IN ("+ params["childs"].value +") ";
query += "   GROUP BY therapy,monthyear   ";
query += "   ORDER BY STR_TO_DATE(cl_sales_planning_month.year_c,'%Y') ASC,   ";
query += "  STR_TO_DATE(cl_sales_planning_month.month_name_c,'%M') ASC, Actual_plan DESC   "; 

为此,我编写了一个 Java 程序:

package com.waprau;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Pattern;

public class SeparateTableNamesColumnNames {
    public static void main(String[] args) {
        File file = new File("/home/waprau/Desktop/query");
        //Pattern = new Pattern("([^\\s]+(\\.(?i))$)");

        try {
            Scanner scanner = new Scanner(file);
            scanner.useDelimiter("\\s|=|,|\\)|\\(|this.|\\].");

            while(scanner.hasNext()){
                if(scanner.next().matches("(?<!\\.)\\b[a-zA-Z]\\w*\\.[a-zA-Z]\\w*\\b(?!\\.)"))
                 System.out.println(scanner.next());;
               }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

使用上述程序,我可以分隔所有单词。但我只想得到那些包含单个点或句点的单词,例如accounts.name、cl_sales_planning_month.year_c、cl_products.volume、cl_brands.name、cl_therapies.name 等。但是我找不到模式或任何可以将这些单词与文件分开。

但它不起作用。

这是我得到的结果:

在此处输入图像描述

这就是我想要的:

在此处输入图像描述

任何帮助表示赞赏。

4

3 回答 3

1

要匹配包含点的单词,您可以使用:"\\w+\\.\\w+"

\w匹配字母、数字和下划线。

但是,这也将匹配具有多个句点的内容。您可以通过使用环视来改进它,以确保在您匹配的单词之前或之后没有另一个句点:

"(?<!\\.)\\b\\w+\\.\\w+\\b(?!\\.)"

这匹配包含点的单词,并且不能在紧接之前或之后包含点。\b是单词边界。

但是,这将匹配十进制数,如123.45. 表格可以包含数字,但不能以 1 开头。所以我们还可以确保每个单词都以字母开头:

"(?<!\\.)\\b[a-zA-Z]\\w*\\.[a-zA-Z]\\w*\\b(?!\\.)"
于 2013-03-04T14:28:54.200 回答
1

该句.点必须转义,因为它意味着“任何字符”。由于这不是正常的字符串转义(如\n),它使用两个反斜杠:\\.

\\s

于 2013-03-04T14:42:47.513 回答
1

无论正则表达式如何(dan1111 的答案似乎涵盖了这一点)。您的 Java 代码存在缺陷,scanner.next() 获取下一个字符串,并且由于您调用它两次,因此您不会打印匹配的内容。相反,您将在每次匹配后打印该项目。

如果您按如下方式更改循环,它似乎会打印您想要的内容:

String tmp;
while (scanner.hasNext()) {
    // Store next item so we can match AND print it.
    tmp = scanner.next();
    if (tmp.matches("(?<!\\.)\\b[a-zA-Z]\\w*\\.[a-zA-Z]\\w*\\b(?!\\.)"))
        System.out.println(tmp);
}
于 2013-03-04T16:07:44.523 回答