提取表名:
对于语句:select f1,f2 from table;
select f1,f2 from table1,table2 where .....
求一正则表达式,能同时上述两种句型,提取出table 字段正则表达式Javaselect
解决方案 »
- 如何设置java中printable打印第二页
- 请教一个 static 和泛型的问题。
- 小弟请问有关JAVA运算符的问题!
- 初学者,学习写猜数字的游戏program.有点疑问请高手帮忙
- appfuse怎么念?(中文发音)
- 请教有关FileInputStream读取资源文件的问题
- 请教一道关于随机数的java题。。。
- 请问怎样在java中定义String数组?:(((这么简单的事都不会!急急急:((
- 请通过SCJP的高手解答,急急急急急急急急急急
- 急!!!请问如何对jar文件进行签名????????
- 为何子类中的覆盖方法只能抛出和基类方法相同或者更少的异常
- 急!急!急,怎么样能最快速的判断大批量数据的剩余次数,具体请看需要,谢谢
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 .....如果是这样呢?
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关键字的情况。
这样没法取的。。不确定性太大了
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));
}
select f1,f2 from table1,table2 where .....from(?<table>[\s\S]*?)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());
}
}
}以前帮人做过类似的。
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));
}
正则只学了个表面,对于一些特殊的限定方法还不是很清楚,谢谢了。