如果前面有 not 或 no,则要排除的正则表达式

regular expression to exclude if preceded by not or no

我如何编写正则表达式来查找前面没有单词 not 或 no 的单词 bad。

在下面的示例中,我应该找到第 1 行和第 4 行。

Line 1: a bad rider

Line 2 : apple is not bad

Line 3 : there is no bad remarks.

Line 4  : no there  is nothing bad

是否可以在没有前瞻的情况下执行此操作,因为它在 Oracle 中不受支持 sql。

尝试:

select *
from table
where regexp_like( column, 'bad' ) and NOT regexp_like( column, '(not|no) bad' )

或者只是(可能是最快的):

select *
from table
where  column like( '%bad%' ) 
  and not (column like '%not bad%' or column like '%no bad%');

我相信这可能会让你更接近一点,并考虑到 "not" 或 "bad" 是另一个词的一部分。正则表达式查找 space 后跟字符串 "not" 或 "no" 后跟 space 然后 "bad" 后跟 space 或结尾该行(确保它们不是另一个词的一部分)。添加更多测试短语,直到您确定为止!即您是否需要允许特殊字符,例如第 5 行是否以问号结尾?

SQL> with tbl(data) as (
  2    select 'Line 1: a bad rider' from dual union
  3    select 'Line 2 : apple is not bad' from dual union
  4    select 'Line 3 : there is no bad remarks.' from dual union
  5    select 'Line 4 : no there is nothing bad' from dual union
  6    select 'Line 5 : was that knot bad' from dual union
  7    select 'Line 6 : let''s play badminton' from dual union
  8    select 'Line 7 : let''s play no badminton' from dual
  9  )
 10  select *
 11  from tbl
 12  where not regexp_like(data, ' (not|no) bad( |$)');

DATA
---------------------------------
Line 1: a bad rider
Line 4 : no there is nothing bad
Line 5 : was that knot bad
Line 6 : let's play badminton
Line 7 : let's play no badminton

SQL>