问题
I am using a regular expression to match where conditions in a SQL query.
I want WHERE <ANY CONDITION>
, but with the exception of WHERE ROWNUM <WHATEVER>
.
So I do not want ROWNUM
to appear after the WHERE
keyword.
I did use Lookaheads to achieve that. My regex is WHERE (.*(?! ROWNUM )+)
. The problem is, it still matches WHERE ROWNUM < 1000
. If I delete the space before ROWNUM
in the regex, then any column with a name ending with ROWNUM
won't match. If I delete the space after WHERE
then it would match even if there is no space after the WHERE
keyword. However, if there are two spaces or any other character between ROWNUM
and the WHERE
keyword (might be a condition), then it is ok. So if ROWNUM
is first in the condition my regex does not work.
How can I fix this ?
回答1:
It sounds like you want
WHERE(?!.*\bROWNUM\b).*
which will match WHERE .*
, unless the .*
contains a ROWNUM
that is surrounded by word boundaries. (\b
, "word boundary", is a zero-width assertion denoting a position that is preceded by a letter or digit or underscore or followed by a letter or digit or underscore, but not both.)
来源:https://stackoverflow.com/questions/9587689/regular-expression-how-to-prevent-match-if-followed-by-a-specific-word-somethin