如何通过正则表达式将select t1.c1,t2.c2 from table1 t1 中select 与 from之间的字符串提取出来或换成其他字符串?

w3svc 2004-06-08 10:18:45
不一定用正则表达式,方法通用就行?我想正则表达式可能简单一些吧?
...全文
405 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
haitangli3 2004-06-08
  • 打赏
  • 举报
回复
用正则表达式会更复杂,因为没有很多个特定的匹配模式
你须抓select from , 这三个关键字
用indexof()即可
得到select from 的位置提取中间的字串
再用,分割分别得到你所要的
happy4nothing 2004-06-08
  • 打赏
  • 举报
回复
select (.*) from table1 t1
这样可以吧
ligenlee 2004-06-08
  • 打赏
  • 举报
回复
只能满足
statment: select <string> from statment
如果有statment: select <string|statment> from statment
那就要复杂一点的算法 那还不如用正则表达式了 还可以把select 0这样的sql语句考虑进来

也可以找出所有的select和from的index 然后一对一对按近距离的原则配对取数
ligenlee 2004-06-08
  • 打赏
  • 举报
回复
String str="select * from (select t1.c1,t2.c2 from (select t1.c1,t2.c2 from table1 t1 ))";
while (str.indexOf("select")!=-1) {
int start=str.indexOf("select")+7;
int end=str.indexOf("from")-1;
String out=str.substring(start,end);
System.out.println("OUT:"+out);
str=str.substring(str.indexOf("from")+4);
}
简单测试了一下 应该可以 但是前提是select 和from要配对的 否则就出错咯
minghuitian 2004-06-08
  • 打赏
  • 举报
回复
gz
w3svc 2004-06-08
  • 打赏
  • 举报
回复
如果是select * from(select t1.c1,t2.c2 from table1 t1)t11 了,该怎么处理?

62,623

社区成员

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

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