postgresql正则表达式

概念

正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。

1、正则匹配

操作符描述
~匹配正则表达式,大小写敏感
~*匹配正则表达式,大小写不敏感
!~不匹配正则表达式,大小写敏感
!~*不匹配正则表达式,大小写不敏感
xieyuheng=# select 'xieyuheng,谢宇恒' ~ '[A-z]'; --字符串包含英文字母
 ?column?
----------
 t
(1 row)

xieyuheng=# select 'xieyuheng' ~ '[^A-z]';--字符串包含不是英文字母的内容
 ?column?
----------
 f
(1 row)

xieyuheng=# select '谢宇恒' ~ '[\u4E00-\u9FA5]';--字符串包含中文
 ?column?
----------
 t
(1 row)

2、正则替换

xieyuheng=# select regexp_replace('xieyuheng,谢宇恒,','[A-z,]','','g');--替换字符串内的英文字母及','为''
 regexp_replace
----------------
 谢宇恒
(1 row)

xieyuheng=# select regexp_replace('xieyuheng,谢宇恒,','[^A-z]','','g');--替换字符串内不是英文字母的内容为''
 regexp_replace
----------------
 xieyuheng
(1 row)

xieyuheng=# select regexp_replace('xieyuheng,谢宇恒,','[^\u4E00-\u9FA5]','','g');--替换字符串内不是中文的内容为''
 regexp_replace
----------------
 谢宇恒
(1 row)

3、正则提取

xieyuheng=# select regexp_matches('xieyuheng,谢宇恒,','[\u4E00-\u9FA5]','g');
 regexp_matches
----------------
 {谢}
 {宇}
 {恒}
(3 rows)

xieyuheng=# select regexp_matches('xieyuheng,谢宇恒,','[A-z]','g');
 regexp_matches
----------------
 {x}
 {i}
 {e}
 {y}
 {u}
 {h}
 {e}
 {n}
 {g}
(9 rows)

xieyuheng=# select regexp_split_to_array('xieyuheng,谢宇恒',',');
 regexp_split_to_array
-----------------------
 {xieyuheng,谢宇恒}
(1 row)

xieyuheng=# select regexp_split_to_table('xieyuheng,谢宇恒',',');
 regexp_split_to_table
-----------------------
 xieyuheng
 谢宇恒
(2 rows)
  • 7
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值