[英]Ruby regex - extract words
I want to extract table names from an SQL query. 我想从SQL查询中提取表名。
SELECT col1, col2, count(1) as count_all, FROM tbl1, tbl2 where condition order by column
I want teh result ["tbl1", "tbl2"]
我想要结果["tbl1", "tbl2"]
It is not necessary that there will be multiple tables to query. 没有必要查询多个表。 In that case the query will be 在那种情况下,查询将是
SELECT col1, col2, count(1) as count_all, FROM tbl1 where condition order by column
And expected result ["tbl1"]
预期结果["tbl1"]
Thanks in advance! 提前致谢!
Note that this can potentially match stuff inside a SQL string and therefore is not perfect. 请注意,这可能会匹配SQL字符串中的内容,因此并不完美。
test = [
"SELECT col1, col2, count(1) as count_all FROM tbl1, tbl2 where condition order by column",
"SELECT col1, col2, count(1) as count_all FROM tbl1 where condition order by column",
"SELECT col1, col2, count(1) as count_all FROM tbl1",
]
tests.map { |str| str.match(/\s*from\s*([a-z_0-9]+(?:,\s*[a-z_0-9]+)*)\b/i); $1 }
#=> ["tbl1, tbl2", "tbl1", "tbl1"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.