提取表名:
  对于语句:select f1,f2 from table;
           select f1,f2 from  table1,table2 where .....
求一正则表达式,能同时上述两种句型,提取出table 字段正则表达式Javaselect

解决方案 »

  1.   

    我要提取的是表名,不是tableX字符串,这是SQL语句,提取任意表名
      

  2.   


    String str="select f1,f2 from table1,table2 where .....";
    Pattern p=Pattern.compile(".*?from (.*?)( |;)");//匹配"from "开头,到“ ”或者”;“结尾
    Matcher m = p.matcher(str);
    while(m.find()){
    System.out.println(m.group(1));
    }
      

  3.   


    select a.f1,b.f2 from  table1 a,table2 b where .....如果是这样呢?
      

  4.   


    select a.f1,b.f2 from  table1 a,table2 b where .....如果是这样呢?
    首先你from后面是两个空格,这不规范,
    另外你这个式子得出的结果就是
    table1 a,table2 b
    这个式子split(",")然后再split(" ")就是两个表的名字
    不要妄图一个正则解决所有问题,正则好用,但是效率不高。
      

  5.   


    select a.f1,b.f2 from  table1 a,table2 b where .....如果是这样呢?
    首先你from后面是两个空格,这不规范,
    另外你这个式子得出的结果就是
    table1 a,table2 b
    这个式子split(",")然后再split(" ")就是两个表的名字
    不要妄图一个正则解决所有问题,正则好用,但是效率不高。我就是那样做的,只是想提取出from 和where之间的表明字段而已,且包含不存在where关键字的情况。
      

  6.   

    select a.f1,b.f2 from  table1 a,table2 b where .....如果是这样呢?
    这样没法取的。。不确定性太大了
      

  7.   


    select a.f1,b.f2 from  table1 a,table2 b where .....如果是这样呢?
    首先你from后面是两个空格,这不规范,
    另外你这个式子得出的结果就是
    table1 a,table2 b
    这个式子split(",")然后再split(" ")就是两个表的名字
    不要妄图一个正则解决所有问题,正则好用,但是效率不高。我就是那样做的,只是想提取出from 和where之间的表明字段而已,且包含不存在where关键字的情况。
    稍微改一下就出来了,欢迎提问,但是建议楼主别光问,看懂了这个式子你自己肯定能改出来的。String str="select a.f1,b.f2 from table1 a,table2 b where .....";
            Pattern p=Pattern.compile(".*?from (.*?)( where|;)");//匹配"from "开头,到“ ”或者”;“结尾,加一个匹配 where就行了。
            Matcher m = p.matcher(str);
            while(m.find()){
                System.out.println(m.group(1));
            }
      

  8.   

    select f1,f2 from table where 1=1;
    select f1,f2 from table1,table2 where .....from(?<table>[\s\S]*?)where
      

  9.   

    Pattern pattern = Pattern.compile("(from +(.*?)(;|where))");
    Matcher matcher = pattern.matcher("select f1,f2 from table1,table2;");
    // .matcher("select f1,f2 from  table1 ,table2 where ……");
    while (matcher.find()) {
    String[] strs = matcher.group(2).split(",| +");
    for (String str : strs) {
    if (!str.equals("")) {
    System.out.println(str.trim());
    }
    }
    }以前帮人做过类似的。
      

  10.   


    select a.f1,b.f2 from  table1 a,table2 b where .....如果是这样呢?
    首先你from后面是两个空格,这不规范,
    另外你这个式子得出的结果就是
    table1 a,table2 b
    这个式子split(",")然后再split(" ")就是两个表的名字
    不要妄图一个正则解决所有问题,正则好用,但是效率不高。
    select a.f1,b.f2 from  table1 a,table2 b where .....如果是这样呢?
    首先你from后面是两个空格,这不规范,
    另外你这个式子得出的结果就是
    table1 a,table2 b
    这个式子split(",")然后再split(" ")就是两个表的名字
    不要妄图一个正则解决所有问题,正则好用,但是效率不高。我就是那样做的,只是想提取出from 和where之间的表明字段而已,且包含不存在where关键字的情况。
    稍微改一下就出来了,欢迎提问,但是建议楼主别光问,看懂了这个式子你自己肯定能改出来的。String str="select a.f1,b.f2 from table1 a,table2 b where .....";
            Pattern p=Pattern.compile(".*?from (.*?)( where|;)");//匹配"from "开头,到“ ”或者”;“结尾,加一个匹配 where就行了。
            Matcher m = p.matcher(str);
            while(m.find()){
                System.out.println(m.group(1));
            }
    正则只学了个表面,对于一些特殊的限定方法还不是很清楚,谢谢了。