使用Oracle正则表达式 - 基于模式的掩码

时间:2012-12-28 05:44:59

标签: regex oracle masking

清理,

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

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

结果应如下所示:“zone”,9(文本“区域”的开头)。

我试图解决的更大问题是使用像'^ .....(。*).. $'这样的模式来屏蔽帐号等数据(此模式可能因安装而异)。

2 个答案:

答案 0 :(得分:0)

下面的内容会对你有用吗?

  

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 :(得分:0)

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;