2017-03-17 21 views

回答

1

使用组()和或|运营商例如验证用户输入您做前人的精力:

set serveroutput on; 
declare 
    l_value varchar2(32767) := '<p>test and data</p>'; 
begin 
    if regexp_like(l_value,'(<|>)','i') then 
    dbms_output.put_line('invalid'); 
    else 
     dbms_output.put_line('valid'); 
    end if; 
end; 
/

好运。

+0

哇! !非常感谢你,那很准确。我正在尝试什么不..再次感谢。 –

+0

嗨,如果我在您的有效案例字符串中使用< or >,它仍然显示有效,但现在应该变为无效。 –

+0

@SachinVaidya请注意,有效的案例已被注释掉,在提供的示例中,我必须编辑答案以防止模棱两可的解释。威廉罗伯逊说,其他变化将起作用。 – Ftaveras

1

使用[]来定义要匹配的一组字符,例如, [abc][a-z],​​

select string 
    , case 
      when regexp_like(string,'[<>]') then 'Invalid' 
      else 'Valid' 
     end as test 
from 
     (select '<p>text</p>' as string from dual union all 
     select 'text' from dual); 

STRING   TEST 
---------------- ------- 
<p>text</p>  Invalid 
text    Valid 

或者在PL/SQL:

declare 
    teststring varchar2(100) := '<p>test and data</p>'; 
    regex  varchar2(100) := '[<>]'; 
begin 
    dbms_output.put('"'||teststring||'"'); 
    dbms_output.put(case when regexp_like(teststring,regex) then ' matches ' else ' does not match ' end); 
    dbms_output.put(regex); 
    dbms_output.new_line(); 
end; 
/

"<p>test and data</p>" matches [<>] 

PL/SQL procedure successfully completed 

作为检查约束:

create table mytable 
(col varchar2(20) 
     constraint mytable_ltgt_chk check (not regexp_like(col,'[<>]')) 
); 

测试:

insert into mytable (col) values ('kitten > puppy'); 

拒绝了:

ORA-02290: check constraint (MYRIAD_OWNER_82.MYTABLE_LTGT_CHK) violated 

如果你想排除方括号内为好,这将是:

constraint mytable_symbol_chk check (not regexp_like(col,'[][<>]')); 

或没有任何正则表达式:

constraint mytable_symbol_chk check (col = translate(col,'[]<>','.')) 

https://regex101.com/r/oQJztM/2/tests

+0

这也非常有帮助。如果我不需要'['或']'符号怎么办?在这种情况下会有帮助吗? –

+0

你的意思是你想检查文本是否包含符号'['或']'?只需将它们包含在正则表达式中即可。 –

+0

嗨,我想要的是,我想对列值设置一个约束,以允许除< or >之外的所有内容。所以我想尝试像ALTER TABLE mytable ADD CONSTRAINT“mytable_CHK”CHECK((REGEXP_LIKE(col,^'(<|>)','i')))。不知道它会以这种方式工作。 –

相关问题