Regular expression how to prevent match if followed by a specific word. Something like first character inclusive lookahead?

耗尽温柔 提交于 2019-12-12 16:24:56

问题


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

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