【问题标题】:regexp replace format all numbers the same in a string (Oracle)正则表达式替换字符串中所有数字的格式相同(Oracle)
【发布时间】:2020-03-28 03:00:19
【问题描述】:

假设我有

“你好,这是 11,我想要 0032,但有 013 和 5”

“您好,这是 0011,我想要 0032,但有 0013 和 0005”

已编辑

【问题讨论】:

  • 你不能拥有它的“任何”长度。替换会导致溢出。
  • 如果我最多需要 4 个这样的句子呢?

标签: oracle regexp-replace


【解决方案1】:

我怀疑单个REGEXP_REPLACE 是否足以完成您的任务,但您可以分两步完成:

  1. 在任何数字前添加一些 0 位,这样至少是您想要的长度。
  2. 删除前导 0 位,直到获得所需长度

代码如下:

SELECT 
  REGEXP_REPLACE(
    REGEXP_REPLACE('Hello this is 11 and i want 0032 but there is 013 and 5'
                  ,'(\d+)','000\1') -- Add three 0-digits to any number
                ,'0+(\d{4})','\1') -- Remove all 0-digits prior to the last 4 digits of any number
  FROM dual

结果:

您好,这是 0011,我想要 0032,但有 0013 和 0005

【讨论】:

    猜你喜欢
    • 2019-04-17
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2012-04-26
    • 2017-02-04
    相关资源
    最近更新 更多