【发布时间】:2016-02-14 22:03:06
【问题描述】:
我有一个查询,想获取from & where 之间的表名
如果它是没有别名的单行单表,我可以这样做:
(?<=from )([^#]\w*)(?=.*where)
我需要获取除前缀表之外的每个表。即course cmarks s
但我无法找出以下查询的正则表达式。
(where 子句可以在同一行或新行,在行首或带有空格或制表符)
from #prefix#student, course c, marks m
where ....
有些地方也有子查询,如果这种情况也能处理会有所帮助。
select ... from course c
where id = (select ... from student where ...)
我正在尝试在sublime text 3 编辑器中查找和替换
测试用例查询:
//output [course]
select ... from course
where ...
//output [course c] [marks s]
select ... from course c, marks s
where ....
//output [marks m]
select ... from #prefix#course c, marks m
where ...
//output [student s]
select ... from #prefix#course c
where id = (select ... from student s where ...)
【问题讨论】:
-
你的预期输出是什么?
-
@AvinashRaj 我已经编辑了问题,请检查。我需要获取所有表名,以便替换它们
-
不确定,但我想你可以试试
\bfrom([^w]*(?:\bw(?!here\b)[^w]*)*)where。 -
如果不想在
from之后匹配任何以#开头的子字符串,我宁愿使用\bfrom(?!\s*#)([^w]*(?:\bw(?!here\b)[^w]*)*)where。 -
@Bsienn 感谢您的回答!我的正则表达式正好相反。排除子查询中的表名。最好尽可能准确地描述问题。
标签: regex sublimetext3