正则表达式如何防止匹配,如果后跟一个特定的单词。有什么像第一个字符包容性的前瞻? [英] Regular expression how to prevent match if followed by a specific word. Something like first character inclusive lookahead?

查看:177
本文介绍了正则表达式如何防止匹配,如果后跟一个特定的单词。有什么像第一个字符包容性的前瞻?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用正则表达式来匹配SQL查询中的条件。

I am using a regular expression to match where conditions in a SQL query.

我想要 WHERE< ANY CONDITION> ,除了 WHERE ROWNUM < WHATEVER>

所以我不希望 ROWNUM 出现在 WHERE 关键字。

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.

我确实使用了 Lookaheads 来实现。我的正则表达式是 WHERE(。*(?!ROWNUM)+)。问题是,它仍然匹配 WHERE ROWNUM< 1000 。如果我在正则表达式中删除 ROWNUM 之前的空格,则名称以 ROWNUM 结尾的任何列将不匹配。如果我删除 WHERE 之后的空格,那么即使在 WHERE 关键字之后没有空格,它也会匹配。但是,如果 ROWNUM WHERE 关键字(可能是条件)之间有两个空格或任何其他字符,那么没关系所以如果 ROWNUM 是第一个条件,我的正则表达式不起作用。

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 ?

推荐答案

听起来像你想要

WHERE(?!.*\bROWNUM\b).*

匹配 WHERE。* 除非 。* 包含一个由字边界包围的ROWNUM \b ,word boundary,是一个零宽度断言,表示前面有字母或数字或下划线的位置后跟一个字母或数字或下划线,,但不能同时使用。)

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.)

这篇关于正则表达式如何防止匹配,如果后跟一个特定的单词。有什么像第一个字符包容性的前瞻?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆