sql - 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 编辑功能)。
非常感谢。
解决方案
您可以尝试以下正则表达式:
regexp_replace(txt, '(\d{4})(\d+(\D|$))', '****\2')
这将捕获 4 位数字的序列,后跟至少一个数字,然后是一个非数字字符(或字符串的结尾),并将它们替换为 4 颗星。
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+'。
推荐阅读
- python - 使用 python 生成器处理 url 分页
- python - 如何使用scrapy在展开更多按钮中提取文本?
- angular - Angular Rxjs:使用管道运算符的语法错误
- php - 如何组合 2 个不同的过滤器
- node.js - 如何将 Paypal 与 node js 集成到 REST 方式中
- javascript - 读取提取数据javascript
- vb6 - 我是否正确实施了单二极管模型?
- python - gunicorn app.wsgi 在 heroku 中实际上在做什么?
- django - 隐藏所有数据集(chartjs)时如何显示比例?
- graphics - 如何生成“草图”动画来介绍 3D 场景?