从SQL查询中提取值,例如字符串



我想提取测试1和this.test2我在下面做的,然后处理右手

 string1:test1 = {{this.test2}}
 String[] parts = string1.split("=");

我打算提取/打印ID1和ID2,product_application,product1,this.product2从上方的字符串中。我可以分开字符串并提取它们:

test1:

id1 IN (SELECT id2 FROM product_application WHERE product1 = {{this.product2}})

test2:

id3 IN (SELECT id4 FROM location WHERE region1 = {{this.region2}}

我想像下面的那样提取或打印。

字符串的输出1:

id1, id2, product_application, product1, product2

字符串2的输出2:

id3, id4, location, region1, region2

这是一个正则可以使用:

String str = 
       "id1 IN (SELECT id2 FROM product_application WHERE product1 = {{this.product2}})";
String regex = "(id\d+)|(product_\w+)|(product\d+)";
Pattern pat = Pattern.compile(regex);
Matcher mat = pat.matcher(str);
List<String> list = new ArrayList<>();
while (mat.find()) {
    list.add(mat.group());
}
System.out.println(list);

输出

[id1, id2, product_application, product1, product2]

编辑

您的字符串有些组合,所以我不知道这是否有帮助,它可以与您的问题中的两个示例一起工作:

String[] spl = str.replaceAll("IN|SELECT|FROM|WHERE|(this\.)|[(){}=,]|(\s{2,})", " ").
        replaceAll("\s+", " ").split("\s");

这个想法是:

  1. 用空间替换所有IN or SELECT or FROM or WHERE or this. or [(){}=,]
  2. 用一个空间替换多个字符串3.将结果放置在空间中,您只会得到结果。

最新更新