我是新使用REGEXP_REPLACE()
。我想将 0
替换为 '-'
数字,例如: 30000000176215001500
我们得到 3-176215001500
我尝试使用 regexp_replace('30000000176215001500','([0])','-')
,但它将所有 0
更改为 -
.
这就是我所期望的:
30000001174934177910
:3-1174934177910
30000000174934177910
:3-174934177910
301873130520
:3-1873130520
300173130520
:3-173130520
最佳答案
考虑:
regexp_replace(mycol,'0+','-', 1, 1)
原理:第五个参数,当大于0时,指定要替换的出现次数;当它设置为 0 时,所有出现的内容都会被替换。
对原始正则表达式的其他显着更改:
- 括号定义捕获组;因为不需要捕获,所以有多余的
- 括号定义字符类;不需要,因为您正在匹配单个字符
<强> Demo on DB Fiddle :
with a as (
select '30000001174934177910' mycol from dual
union all select '30000000174934177910' from dual
union all select '301873130520' from dual
union all select '300173130520' from dual
)
select mycol input, regexp_replace(mycol,'0+','-', 1, 1) output from a
INPUT | OUTPUT :------------------- | :-------------- 30000001174934177910 | 3-1174934177910 30000000174934177910 | 3-174934177910 301873130520 | 3-1873130520 300173130520 | 3-173130520
关于sql - 在字符前后使用 regexp_replace 替换字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58035455/