[英]Regex to match words separated by one dot after particular set of words until whitespace
I need to grab the schemaName.tableName from the string containing SQL query.我需要从包含 SQL 查询的字符串中获取 schemaName.tableName。
For example例如
Select t1.column1, t1.column2, t2.column3, to.column4 from ABC.TABLE1 t1 inner join XYZ.TABLE2 t2 where t1.id=t2.id;
After regex match I should get an array containing schemaName.tableName正则表达式匹配后,我应该得到一个包含 schemaName.tableName 的数组
[ABC.TABLE1,XYZ.TABLE2] [ABC.TABLE1,XYZ.TABLE2]
This pattern should be able to fetch schemaName.tableName from any SQL query.此模式应该能够从任何 SQL 查询中获取 schemaName.tableName。
How can I create this pattern please help me.我怎样才能创建这个模式请帮助我。
I tried following but didnt got result我尝试关注但没有得到结果
https://regex101.com/r/D82EJh/1 https://regex101.com/r/D82EJh/1
[^\/,\s\?#]+?\.[^\/,\s]+?(?=\/|\s|$|\?|#)
This pattern should be able to fetch schemaName.tableName from any SQL query.此模式应该能够从任何 SQL 查询中获取 schemaName.tableName。
To complete the task in this formulation, you will need to organize a whole project, the success of which will nevertheless depend on the chosen dialect of the sql language.要完成此公式中的任务,您将需要组织一个完整的项目,其成功与否仍将取决于所选择的 sql 语言的方言。 Perhaps you will be satisfied with solving a simpler problem: how to get an array of words (including dots) that follow the words "from" or "join".也许您会满足于解决一个更简单的问题:如何获取“from”或“join”之后的单词数组(包括点)。 Then I propose such a solution below.那么我在下面提出这样的解决方案。
const sql = "Select t1.column1, t1.column2, t2.column3, to.column4 from ABC.TABLE1 t1 inner join XYZ.TABLE2 t2 where t1.id=t2.id;";
console.log(sql);
const regex = /(?<=\b(from|join)\s)(\S+)/g;
const found = sql.match(regex);
console.log(found);
This code should show in the console: ['ABC.TABLE1','XYZ.TABLE2']此代码应显示在控制台中: ['ABC.TABLE1','XYZ.TABLE2']
You could use a capture group, and match the allowed characters:您可以使用捕获组,并匹配允许的字符:
\b(?:join|from)\s+(\w+\.\w+)
Explanation解释
\b
A word boundary \b
单词边界(?:join|from)\s+
Match either join or from followed by 1 or more whitespace characters (?:join|from)\s+
匹配 join 或 from 后跟 1 个或多个空白字符(
Capture group 1 (that holds the desired value) (
捕获组 1 (包含所需的值)
\w+\.\w+
Match 1 or more word characters, then a dot and again 1.or more word characters \w+\.\w+
匹配 1 个或多个单词字符,然后是一个点,然后再匹配 1 个或多个单词字符)
Close group 1 )
关闭组 1See a regex101 demo .请参阅regex101 演示。
If there should be 1 dot and no more non whitespace characters after the word characters, you could assert a whitespace boundary:如果单词字符后应该有 1 个点并且不再有非空白字符,则可以断言空白边界:
\b(?:join|from)\s+(\w+\.\w+)(?!\S)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.