使用基于模式的Oracle正则表达式屏蔽



清理,

在Oracle 11g PL/SQL中,对于下面的查询,可以获得捕获组的位置(类似于java中的Matcher.start())。

    `select regexp_replace('1234bankzone1234', '^..(.*)bank(zone).(.*)..$', '2') from dual`

结果应该看起来像:"zone", 9(文本"zone"的开始)。

我试图解决的更大的问题是使用像'^.....(.*)..

下面的东西对你有用吗?

select regexp_replace('1234bankzone1234', '^..(.*)bank(zone).(.*)..$', '2') expr ,instr('1234bankzone1234',regexp_replace('1234bankzone1234', '^..(.*)bank(zone).(.*)..$', '2')) pos from dual

或更可读的子查询,如

select a.*, instr(a.value,a.expr) from ( select '1234bankzone1234' value, regexp_replace('1234bankzone1234', '^..(.*)bank(zone).(.*)..$', '2') expr from dual ) a

我找不到任何类似Matcher API的功能,也没有办法在SQL中访问位置组缓冲区。

1:使用此

regexp_replace( regexp_replace( regexp_replace( regexp_replace( regexp_replace( regexp_replace( regexp_replace( regexp_replace( regexp_replace( pattern, '(()', '1#') , '())', '#1') , '(#', ')#') , '^)#', '^') , '#)$', '$') , '#)', '(#') , '#', '') , '^([^(]+))', '^(1') , '(([^)]+)$', '(1)$');

^ (

。")..(.).$";成为"^ . (..).(.)$";

2:使用它来批量收集两个模式中捕获组的索引和计数

SELECT REGEXP_instr(pattern, '(.*?)+', 1, LEVEL) bulk collect into posCapture FROM v CONNECT BY LEVEL <= REGEXP_COUNT(pattern, '(.*?)');

3:将两个模式与要屏蔽的文本进行匹配。按步骤2中的顺序合并。

select regexp_replace(v_src, pattern, '' || captureIndex) into tempStr from dual;

最新更新