如何在oracle中转义特殊的正则表达式字符?

时间:2013-09-20 11:56:15

标签: regex oracle plsql

如何在Oracle中转义正则表达式模式的特殊字符?

例如,我需要一个traslates

的函数

some.string[with(special)reg-exp]characters

some\.string\[with\(special\)reg\-exp\]characters

在PHP中我会使用preg_escape()。是否有Oracle对手?

为什么我这样做?

我正在尝试编写一个pl / sql函数来检查string上是否有list,of,string,elements

这是我的代码:

CREATE OR REPLACE 
FUNCTION list_contains(needle_    IN VARCHAR2,
                       haystack_  IN VARCHAR2,
                       separator_ IN VARCHAR2 DEFAULT ',')
RETURN INTEGER
  IS
BEGIN
  IF regexp_like(haystack_, '(^|' || separator_ || ')' || needle_ || '(' || separator_ || '|$)') THEN
    RETURN 1;
  ELSE
    RETURN 0;
  END IF;
END list_contains;

该功能基本有效:

list_conains('eve','john,eve,maria,steve') => 1

问题是当我尝试使用needle_separator_的奇怪值(例如.)或其他在正则表达式中具有特殊含义的字符串来调用它时。

list_conains('eve','john.maria.steve','。')=> 1

如您所见,列表中没有前夕,但.与史蒂夫名称的t字母匹配,因此函数错误地返回1

我知道我可以手动替换这些点,但是还有很多其他的正则表达式特殊字符会干扰,我宁愿不尝试自己列出所有字符。

如何逃脱needle_和分隔符_?

5 个答案:

答案 0 :(得分:2)

如果我理解你的问题,你不需要正则表达式。你可以使用简单的LIKE。

IF separator_ || haystack_ || separator_ LIKE '%'||separator_||needle_||separator_||'%' THEN
   RETURN 1;
ELSE
   RETURN 0;
END IF;

编辑:如果%_本身就是separator_,则必须将其转义。

IF separator_ = '%' OR separator_ = '_'
THEN
  separator_ := '\' || separator_;
END IF;

IF separator_ || haystack_ || separator_ LIKE
     '%' || separator_ || needle_ || separator_ || '%' ESCAPE '\'
THEN
  RETURN 1;
ELSE
  RETURN 0;
END IF;

答案 1 :(得分:1)

必须转义以下字符:\ ^。 $ | ()[] * +? {},

http://psoug.org/snippet/Regular-Expressions--Regexp-Cheat-Sheet_856.htm

SELECT REGEXP_REPLACE('some.string[with(special)reg-exp]characters', '([][)(}{.$*+?,|^\])', '\\\1') "REGEXP_REPLACE" FROM dual;

答案 2 :(得分:0)

我认为在regexp语句中转义分隔符可能最容易。试试这个,它似乎对我有用:

  IF regexp_like(haystack_, '(^|\' || separator_ || ')' || needle_ || '(\' || separator_ || '|$)') THEN

我改变的只是字符串结构中分隔符前面的斜线。

答案 3 :(得分:0)

试试这个:

CREATE OR REPLACE 
FUNCTION list_contains(needle_    IN VARCHAR2,
                       haystack_  IN VARCHAR2,
                       separator_ IN VARCHAR2 DEFAULT ',')
return number AS
  l_return_count number := 0;
BEGIN
  with haystack_ary as (
    select extractvalue(x.column_value, 'e') as val
    from xmltable ('e' passing xmlparse( content  '<e>' || replace(haystack_, separator_, '</e><e>') || '</e>')) x
  )
  select 
  --count(1)
  --return as a "bool"(1=true,0=false)
  decode(count(1), 0, 0, 1)
  into l_return_count
  from haystack_ary
  where lower(needle_) = lower(haystack_ary.val);

  return l_return_count;
END;

我用较低的功能使其不区分大小写。如果您愿意,还可以修剪空格:lower(trim(needle_)) = lower(trim(haystack_ary.val))

答案 4 :(得分:0)

此操作无需任何正则表达式即可完成,并通过 instr 函数转义,如果不匹配,则将返回0 ,如果匹配,将> 0

为此,您应在 needle _ haystack _ 的开头和结尾处添加分隔符,然后检查 haystack _ 包含 needle _

概念证明

select haystack_, needle_, separator_, instr(separator_||haystack_||separator_, separator_||needle_||separator_) result_, expected_
from (
    select 'john,eve,maria,steve' as haystack_ , 'eve' as needle_, ',' as separator_, '>0'as expected_ from dual union all
    select 'john,eve,maria,steve' as haystack_ , 'john' as needle_, ',' as separator_, '>0'as expected_ from dual union all
    select 'john,eve,maria,steve' as haystack_ , 'joh' as needle_, ',' as separator_, '=0'as expected_ from dual union all
    select 'john,eve,maria,steve' as haystack_ , 'steve' as needle_, ',' as separator_, '>0'as expected_ from dual union all
    select 'john,eve,maria,steve' as haystack_ , 'stev' as needle_, ',' as separator_, '=0'as expected_ from dual union all
    select 'john,eve,maria,steve' as haystack_ , 'teve' as needle_, ',' as separator_, '=0'as expected_ from dual union all
    select 'john.maria.steve' as haystack_ , 'eve' as needle_, '.' as separator_, '=0'as expected_ from dual union all
    select 'john_maria_steve' as haystack_ , 'eve' as needle_, '_' as separator_, '=0'as expected_ from dual union all
    select 'john%maria%steve' as haystack_ , 'eve' as needle_, '%' as separator_, '=0'as expected_ from dual
) t;

结果:

HAYSTACK_            NEEDLE_ SEPARATOR_ RESULT_ EXPECTED_
john,eve,maria,steve eve     ,          6       >0
john,eve,maria,steve john    ,          1       >0
john,eve,maria,steve joh     ,          0       =0
john,eve,maria,steve steve   ,          16      >0
john,eve,maria,steve stev    ,          0       =0
john,eve,maria,steve teve    ,          0       =0
john.maria.steve     eve     .          0       =0
john_maria_steve     eve     _          0       =0
john%maria%steve     eve     %          0       =0