2016-11-22 200 views
0

我如何编写正则表达式来找到不是前面没有单词不是或不是的单词坏。正则表达式排除,如果前面没有或没有

在下面我应该找到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支持。

+0

使用['(?<!不)(?<!没有)\ BBAD \ B'(https://regex101.com/r/ywMEV6/1) –

+0

我需要做的是在甲骨文正规快递。消极的前瞻似乎不被支持。是否有可能做到没有负面看法? – snowrock

+1

不,在纯Oracle正则表达式中,这是不可能的。 –

回答

-1

尝试:

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%'); 
+0

再次感谢。它按预期工作。 – snowrock

+0

添加更多测试数据!用句子说句“结不好?”。测试时,在您的非/非的两侧添加空格。对于这个问题“坏”,因为它可能是另一个词的一部分。 –

0

我相信这可以让你更接近一点,允许时“不”或“坏”的一部分的另一个词。正则表达式寻找一个空格,后跟字符串“不”或“否”,后跟空格,然后是“坏”,后跟空格或行尾(确保它们不是另一个单词的一部分)。添加更多的测试短语,直到您确定!即您是否需要允许特殊字符,如第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>