Oracle中金额替换方式,oracle使用regexp_replace实现金额格式统一

背景

1个月前有个社区的网友在微信群里问下面格式的金额如何统一成数字,

62cd7f0dbd4d344d1e266f14a7c8d151.png

当时并没有想出来很方便的方法,就用replace的非常笨的办法,把所有的字符用ascii穷举出来,然后替换为空

我们都知道regexp_replace这个函数是replace的进阶版本,用法和replace基本一样,但是

regexp_replace允许使用POSIX 正则表达式来匹配

regexp_replace298bf813ef554680439e465c46a9f1de.gif

Purpose

REGEXP_REPLACE extends the functionality of the REPLACE function by letting you search a string for a regular expression pattern. By default, the function returns source_char with every occurrence of the regular expression pattern replaced with replace_string. The string returned is in the same character set as source_char. The function returns VARCHAR2 if the first argument is not a LOB and returns CLOB if the first argument is a LOB.

说到底还是自己对正则表达式不够6,今天查资料查到一个很方便的关键字:[[:alpha:]]可以直接匹配所有的字符

实验with ltz as

(

select '12.3元' as amount from dual union all

select '12元' from dual union all

select '人民币12元' from dual union all

select '人民币12' from dual union all

select '12' from dual union all

select '十二元' from dual

)

select t.*,regexp_replace(t.amount,'[[:alpha:]]') as result from ltz t

2fcdaeb0208e972681927b62f743df18.png

需求二:手机号码替换成中间6位星号,手机号码规则:11位数字,且第一位为1with ltz as

(select '老头子18665801235' n from dual union all

select 'Azzo18665801236' from dual union all

select '老子18665801237头' from dual union all

select '_18665801235chen' from dual union all

select '_28665801235chen' from dual union all

select '1A8B6C6D5E8F0G5235chen' from dual union all

select '辣条186' from dual )

select n,

case when (substr(regexp_replace(n,'[[:alpha:][:punct:]]',''),1,1) = 1

and length(regexp_replace(n,'[[:alpha:][:punct:]]','')) = 11

and instr(n,regexp_replace(n,'[[:alpha:][:punct:]]',''))<> 0)

then substr(n,1,instr(n,regexp_replace(n,'[[:alpha:][:punct:]]',''))+2)||'******'||

substr(n,instr(n,regexp_replace(n,'[[:alpha:][:punct:]]',''))+9)

else n

end as result

from ltz

d63c998e5f25579a4282379be392b903.png

需求三:去掉中文、数字、标点with ltz as (

select 'laotouzi真帅' as t from dual union all

select 'laotouzizhen shuai!' from dual union all

select '老头子zhenshuai' from dual union all

select '老tou子真帅' from dual union all

select '老头子真帅!!!a' from dual union all

select '老头子123真帅hehe!!!' from dual

)

select regexp_replace(regexp_replace(asciistr(t),'\\[[:alnum:]]{4}'),'[[:punct:][:digit:]]') from ltz

c6f319e7e1e7caec5dc03de1de566302.png

知识拓展

Oracle使用POSIX标准的正则表达式,下面是部分字符集的标准,供参考

79b2dd63af8db059d55fa979ba6c3e9b.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值