簡體   English   中英

Ruby正則表達式 - 提取單詞

[英]Ruby regex - extract words

我想從SQL查詢中提取表名。

SELECT col1, col2, count(1) as count_all,     FROM     tbl1, tbl2 where condition order by column

我想要結果["tbl1", "tbl2"]

沒有必要查詢多個表。 在那種情況下,查詢將是

SELECT col1, col2, count(1) as count_all,     FROM     tbl1  where condition order by column

預期結果["tbl1"]

提前致謝!

請注意,這可能會匹配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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM