Regex to extract info from SQL query

て烟熏妆下的殇ゞ 提交于 2019-12-11 08:47:06

问题


As I am new for the REGEX i am not able to solve below thing.

And please share some parser related links so the i can learn it.

I am facing problem in solving int below SQL statement. Its more line added to the previous INPUT.

Please help me to slove this.

DECLARE
numerator   NUMBER;
BEGIN
SELECT x, y INTO numerator, denominator FROM result_table, s_Table
WHERE sample_id = 8;
the_ratio := numerator/denominator;
IF the_ratio > lower_limit THEN
INSERT INTO 
ratio VALUES (table, coloum);
ELSE
INSERT INTO onemoreTable VALUES (table, -1);
END IF;
COMMIT;
delete from     --some comment
xyz where id=17;
EXCEPTION
WHEN ZERO_DIVIDE THEN
INSERT INTO ratio VALUES (table, 0);
COMMIT;
WHEN OTHERS THEN
ROLLBACK;
END;

OUTPUT:

SELECT from: result_table, s_Table
INSERT into: ratio
INSERT into: onemoreTable
DELETE from: xyz
INSERT into: ratio

回答1:


Here is a Perl based solution using regex:

$input =~s/--.*?\n//g; # delete the comments.
$input =~s/\s+/ /g; # replace multiple white space with single space.
while($input=~m/((?:insert into)|(?:delete from)) (\w+)/ig) { 
        print "$1 : $2\n";    
}

The right way to parse SQL is to do it using a parser and not using regex.



来源:https://stackoverflow.com/questions/3563071/regex-to-extract-info-from-sql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!