Using regular expression to find character NOT preceded by '--'
649496Sep 30 2008 — edited Oct 1 2008I have a table with a column called TEST_CODE, which contains actual SQL statements and PL/SQL code (which is executed by another procedure). The code can't contain any ampersands (&), which cause Oracle to prompt the user to enter a value. So far, I've just checked the code using: "IF test_code LIKE '%&%' THEN [raise exception]'
Recently, we've found a need to allow values in TEST_CODE to contain an ampersand if it is part of a comment, or in a section of code that has been commented out. I've been trying to come up with a REGEXP_LIKE expression that would find any string containing '&' but only if it is not preceded by two consecutive dashes '--' on the same line.
So it should find:
'SELECT *
FROM &p_tab -- comment '
'SELECT * -- & comment
FROM &p_tab'
'SELECT * -- & comment
FROM &p_tab -- & comment '
'SELECT * -- comment
FROM &p_tab -- & comment '
But not:
'SELECT *
FROM t1'
'SELECT * -- & comment
FROM t1'
'SELECT * -- & comment
FROM t1 -- & comment '
It seemed easy enough until I tried to do it (and I haven't even tried to do anything with multi-line comments yet!). Can anyone help?