正则表达式使用Java在sql查询中查找表别名和字段

时间:2022-10-21 19:53:23

I have a string which contains a very long and complex query.

我有一个字符串,其中包含一个非常长而复杂的查询。

What I want is that one of my tables has a alias T_1; I would like to get all the literals in the query which are referred through T_1.

我想要的是我的一张桌子有一个别名T_1;我想获得查询中通过T_1引用的所有文字。

For example, if the query is

例如,如果查询是

Select T_1.Name, 
       T_1.Age 
  from Demo T_1,
       Table2 T_2 
 where T_1.EmpId = T_2.EmpId;

then I should match the following strings:

那么我应该匹配以下字符串:

  1. Name
  2. Age
  3. EmpId

1 个解决方案

#1


1  

The question was already answered by Avinash Raj in the comments, but here the complete answer:

Avinash Raj在评论中已经回答了这个问题,但这里有完整的答案:

Assuming you are using java the code you need is something like this:

假设您正在使用java,您需要的代码是这样的:

Pattern p = Pattern.compile("(?<=\\.)\\w+");
Matcher m = p.matcher("Select T_1.Name, \n" +
        "       T_1.Age \n" +
        "  from Demo T_1,\n" +
        "       Table2 T_2 \n" +
        " where T_1.EmpId = T_2.EmpId;");
while (m.find()) {
    System.out.println(m.group());
}

#1


1  

The question was already answered by Avinash Raj in the comments, but here the complete answer:

Avinash Raj在评论中已经回答了这个问题,但这里有完整的答案:

Assuming you are using java the code you need is something like this:

假设您正在使用java,您需要的代码是这样的:

Pattern p = Pattern.compile("(?<=\\.)\\w+");
Matcher m = p.matcher("Select T_1.Name, \n" +
        "       T_1.Age \n" +
        "  from Demo T_1,\n" +
        "       Table2 T_2 \n" +
        " where T_1.EmpId = T_2.EmpId;");
while (m.find()) {
    System.out.println(m.group());
}