正则表达式替换字符串中所有数字相同的格式 (Oracle)

regexp replace format all numbers the same in a string (Oracle)

假设我有

"Hello this is 11 and i want 0032 but there is 013 and 5"

"Hello this is 0011 and i want 0032 but there is 0013 and 0005"

已编辑

我怀疑单个 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') -- Add three 0-digits to any number
                ,'0+(\d{4})','') -- Remove all 0-digits prior to the last 4 digits of any number
  FROM dual

结果:

Hello this is 0011 and i want 0032 but there is 0013 and 0005