java求一个正则表达式

qq631032049 2013-08-08 03:32:19

提取表名:
对于语句:select f1,f2 from table;
select f1,f2 from table1,table2 where .....
求一正则表达式,能同时上述两种句型,提取出table 字段
...全文
197 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq631032049 2013-08-09
  • 打赏
  • 举报
回复
引用 6 楼 AA5279AA 的回复:
[quote=引用 4 楼 qq631032049 的回复:] [quote=引用 3 楼 AA5279AA 的回复:]

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));
		}
select a.f1,b.f2 from table1 a,table2 b where ..... 如果是这样呢?[/quote] 首先你from后面是两个空格,这不规范, 另外你这个式子得出的结果就是 table1 a,table2 b 这个式子split(",")然后再split(" ")就是两个表的名字 不要妄图一个正则解决所有问题,正则好用,但是效率不高。[/quote]
引用 9 楼 AA5279AA 的回复:
[quote=引用 7 楼 qq631032049 的回复:] [quote=引用 6 楼 AA5279AA 的回复:] [quote=引用 4 楼 qq631032049 的回复:] [quote=引用 3 楼 AA5279AA 的回复:]

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));
		}
select a.f1,b.f2 from table1 a,table2 b where ..... 如果是这样呢?[/quote] 首先你from后面是两个空格,这不规范, 另外你这个式子得出的结果就是 table1 a,table2 b 这个式子split(",")然后再split(" ")就是两个表的名字 不要妄图一个正则解决所有问题,正则好用,但是效率不高。[/quote] 我就是那样做的,只是想提取出from 和where之间的表明字段而已,且包含不存在where关键字的情况。[/quote] 稍微改一下就出来了,欢迎提问,但是建议楼主别光问,看懂了这个式子你自己肯定能改出来的。

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));
        }
[/quote] 正则只学了个表面,对于一些特殊的限定方法还不是很清楚,谢谢了。
  • 打赏
  • 举报
回复
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());
				}
			}
		}
以前帮人做过类似的。
再来壹串 2013-08-08
  • 打赏
  • 举报
回复
select f1,f2 from table where 1=1; select f1,f2 from table1,table2 where ..... from(?<table>[\s\S]*?)where
失落夏天 2013-08-08
  • 打赏
  • 举报
回复
引用 7 楼 qq631032049 的回复:
[quote=引用 6 楼 AA5279AA 的回复:] [quote=引用 4 楼 qq631032049 的回复:] [quote=引用 3 楼 AA5279AA 的回复:]

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));
		}
select a.f1,b.f2 from table1 a,table2 b where ..... 如果是这样呢?[/quote] 首先你from后面是两个空格,这不规范, 另外你这个式子得出的结果就是 table1 a,table2 b 这个式子split(",")然后再split(" ")就是两个表的名字 不要妄图一个正则解决所有问题,正则好用,但是效率不高。[/quote] 我就是那样做的,只是想提取出from 和where之间的表明字段而已,且包含不存在where关键字的情况。[/quote] 稍微改一下就出来了,欢迎提问,但是建议楼主别光问,看懂了这个式子你自己肯定能改出来的。

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));
        }
BigQiu66 2013-08-08
  • 打赏
  • 举报
回复
引用 4 楼 qq631032049 的回复:
[quote=引用 3 楼 AA5279AA 的回复:]

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));
		}
select a.f1,b.f2 from table1 a,table2 b where ..... 如果是这样呢?[/quote] 这样没法取的。。不确定性太大了
qq631032049 2013-08-08
  • 打赏
  • 举报
回复
引用 6 楼 AA5279AA 的回复:
[quote=引用 4 楼 qq631032049 的回复:] [quote=引用 3 楼 AA5279AA 的回复:]

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));
		}
select a.f1,b.f2 from table1 a,table2 b where ..... 如果是这样呢?[/quote] 首先你from后面是两个空格,这不规范, 另外你这个式子得出的结果就是 table1 a,table2 b 这个式子split(",")然后再split(" ")就是两个表的名字 不要妄图一个正则解决所有问题,正则好用,但是效率不高。[/quote] 我就是那样做的,只是想提取出from 和where之间的表明字段而已,且包含不存在where关键字的情况。
失落夏天 2013-08-08
  • 打赏
  • 举报
回复
引用 4 楼 qq631032049 的回复:
[quote=引用 3 楼 AA5279AA 的回复:]

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));
		}
select a.f1,b.f2 from table1 a,table2 b where ..... 如果是这样呢?[/quote] 首先你from后面是两个空格,这不规范, 另外你这个式子得出的结果就是 table1 a,table2 b 这个式子split(",")然后再split(" ")就是两个表的名字 不要妄图一个正则解决所有问题,正则好用,但是效率不高。
末日哥 2013-08-08
  • 打赏
  • 举报
回复
你表名没什么特征?那就难了
qq631032049 2013-08-08
  • 打赏
  • 举报
回复
引用 3 楼 AA5279AA 的回复:

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));
		}
select a.f1,b.f2 from table1 a,table2 b where ..... 如果是这样呢?
失落夏天 2013-08-08
  • 打赏
  • 举报
回复

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));
		}
qq631032049 2013-08-08
  • 打赏
  • 举报
回复
我要提取的是表名,不是tableX字符串,这是SQL语句,提取任意表名
末日哥 2013-08-08
  • 打赏
  • 举报
回复
table\\d*

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧