首页 > 解决方案 > Oracle SQL - 在自由文本叙述中编辑除了最后四位数字之外的多次出现

问题描述

是否有直接的方法(可能使用 REGEXP_REPLACE 等)来编辑出现在自由文本中的除最后四位数字(或长度为 5 或以上)之外的所有数字(文本中可能多次出现单独的数字)?

例如

Input = 'This is a test text with numbers 12345, 9876543210 and separately number 1234567887654321 all buried within the text'

Output = 'This is a test text with numbers ****5, *****3210 and separately number ************4321 all buried within the text'

使用 REGEX_REPLACE,用 * 替换所有数字显然很简单,但它会保留最后四位数字并用正确数量的 * 替换,这让我很烦恼。

任何帮助将非常感激!

(只是为了上下文,由于通常的业务限制,这必须在检索数据的查询中完成,而不是使用实际的 Oracle DBMS 编辑功能)。

非常感谢。

标签: sqlregexstringoracleredaction

解决方案


您可以尝试以下正则表达式:

regexp_replace(txt, '(\d{4})(\d+(\D|$))', '****\2')

这将捕获 4 位数字的序列,后跟至少一个数字,然后是一个非数字字符(或字符串的结尾),并将它们替换为 4 颗星。

DB Fiddle 上的演示

with t as (select 'select This is a test text with numbers 12345, 9876543210 and separately number 1234567887654321 all buried within the text' txt from dual)
select regexp_replace(txt, '(\d{4})(\d+\D)', '****\2') new_text from t
| 新文本 |
| :------------------------------------------------ -------------------------------------------------- ------------------------------------ |
| 选择这是一个测试文本,数字****5、****543210 和单独的数字****567887654321 都埋在文本中 |

编辑

这是 Aleksej 在评论中建议的简化版本:

regexp_replace(txt, '(\d{4})(\d+)', '****\2')

这是因为正则表达式引擎的贪婪,它会尽可能多地吞下 '\d+'。


推荐阅读