2017-03-17 23 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')))。不知道它會以這種方式工作。 –

相關問題