gpt4 book ai didi

java - 如何在包含单个点或句点的文件中获取单词?

转载 作者:行者123 更新时间:2023-11-29 05:47:44 26 4
gpt4 key购买 nike

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

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 等。但是我无法找到可以将这些词与文件分开的模式或任何内容。

但它不起作用。

这是我得到的结果:

enter image description here

这就是我想要的:

enter image description here

感谢任何帮助。

最佳答案

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

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

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

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

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

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

"(?<!\\.)\\b[a-zA-Z]\\w*\\.[a-zA-Z]\\w*\\b(?!\\.)"

关于java - 如何在包含单个点或句点的文件中获取单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15203759/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com