Regex to extract info from SQL query
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
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.
精彩评论