我有一个oracle表,它的列是varchar2(4000)。我希望能够找到9个连续的数字,并将其替换为xxxxxxxxx。如果有超过9个连续的数字,我不想替换这些数字。我试过了
regexp_replace('this is my string of digits 987654321889890','\d{9}','xxxxxxxxx')但是这将返回'this is my string of digits xxxxxxxxx889890'
在这种情况下,我希望regexp引擎忽略任何超过9个连续数字的子字符串。
先谢谢你乔伊
发布于 2014-12-12 05:27:19
这应该会产生预期的结果:
regexp_replace(str, '(^|[^0-9])([0-9]{9})($|[^0-9])', '\1xxxxxxxxx\3')有关实时示例,请参阅http://sqlfiddle.com/#!4/d41d8/38554
发布于 2014-12-12 06:56:02
Sylvain已经发布了类似的答案。
使用11.1Oracle数据库,可以将字符分组应用到替换模式中。参见here。
像Sylvain一样,关键是创建一个由字符集封装的匹配模式,可以用“xxxxxxxxx”替换九个重复的数字。
解决方案的步骤:
以下是我的示例解决方案:
SCOTT@dev> list
1 WITH t AS
2 (SELECT 'this is my string of digits 987654321889890' txt_val FROM dual
3 UNION
4 SELECT 'this is my string of digits 54321889890' FROM dual
5 UNION
6 SELECT 'this is my string of digits 987654321' FROM dual
7 UNION
8 SELECT 'this is my string of digits 98761189890' FROM dual
9 UNION
10 SELECT 'this is my string of digits 121889890' FROM dual
11 )
12 SELECT t.txt_val,
13 regexp_replace(t.txt_val,'([^0-9]|^)(\d{9})([^0-9]|$)','\1xxxxxxxxx\3') txt_val_fixed
14* FROM t
SCOTT@dev> /
TXT_VAL TXT_VAL_FIXED
=========================================== ================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
this is my string of digits 121889890 this is my string of digits xxxxxxxxx
this is my string of digits 54321889890 this is my string of digits 54321889890
this is my string of digits 98761189890 this is my string of digits 98761189890
this is my string of digits 987654321 this is my string of digits xxxxxxxxx
this is my string of digits 987654321889890 this is my string of digits 987654321889890发布于 2014-12-13 03:12:29
感谢大家提供的有用信息。我最终使用了
JAM@DEV>select regexp_replace(‘987654653这是我的号码950569808890现在不是878788976,这个号码小于8823727再加一个950877665','(\D|^)(\d{9})(\D|$)',’xxx-xx-xxxx ',1,0,'m') "regexpreplace“from dual;
regexpreplace
他是我的号码950569808890,不是xxx-xx-xxxx,这个小于8823727,再加一。
https://stackoverflow.com/questions/27427927
复制相似问题