正则案例二:匹配有且仅有3个连续数字的字符

一.需求描述

近期收到朋友的一个需求,需要在一堆字符串中查找连续3个数字的字符,连续数字可能出现在开头、中间、结尾,而且如果连续数字超过3个,也不符合条件。

二.解决方案

2.1 Oracle解决方案

^ 匹配每一行的开头,单行模式下等价于字符串的开头
$ 匹配每一行的结尾,单行模式下等价于字符串的结尾

字母可以使用 [a-zA-Z]
数字可以用 \d 或者 [0-9]

我测试匹配开头使用\d居然未匹配出来,只能使用[0-9]

代码:

with tmp1 as
(
select 'abc12' str from dual
union 
select 'abc123' str from dual  -- OK 
union 
select 'abc123ab' str from dual  -- OK 
union 
select '123abc' str from dual   -- OK 
union
select '12abc123' str from dual  -- OK 
union 
select '12abc1234' str from dual
union 
select '1234abc12' str from dual
union
select '123' str from dual      -- OK
)
select *
  from tmp1
 where regexp_like(str, '/*?\D{1}\d{3}\D{1}\/*?')  -- 匹配连续数字出现在中间,有且仅有3个数字
   or  regexp_like(str, '*?\D{1}\d{3}$')           -- 匹配连续数字出现在结尾
   or  regexp_like(str, '^[0-9]{3}\D{1}\*?')       -- 匹配连续数字出现在开头
   or  regexp_like(str,'^[0-9]{3}$')               -- 匹配有且仅有3个数字的字符串

测试记录:

with tmp1 as
(
select 'abc12' str from dual
union 
select 'abc123' str from dual  -- OK 
union 
select 'abc123ab' str from dual  -- OK 
union 
select '123abc' str from dual   -- OK 
union
select '12abc123' str from dual  -- OK 
union 
select '12abc1234' str from dual
union 
select '1234abc12' str from dual
union
select '123' str from dual      -- OK
)
select *
  from tmp1
 where regexp_like(str, '/*?\D{1}\d{3}\D{1}\/*?')  -- 匹配连续数字出现在中间,有且仅有3个数字
   or  regexp_like(str, '*?\D{1}\d{3}$')           -- 匹配连续数字出现在结尾
   or  regexp_like(str, '^[0-9]{3}\D{1}\*?')       -- 匹配连续数字出现在开头
   or  regexp_like(str,'^[0-9]{3}$')               -- 匹配有且仅有3个数字的字符串

2.2 MySQL解决方案

MySQL的正则与Oracle的语法存在差异,需要重新写。

^ 匹配每一行的开头,单行模式下等价于字符串的开头
$ 匹配每一行的结尾,单行模式下等价于字符串的结尾

数字用 [0-9]
非数字用 [^0-9]
匹配多个表达式需要用[A]+[B] 其中[]是一个匹配的集合,“+”号是连接的意思。

匹配非字符次数的时候可以使用[^0-9]{0,} 表示匹配非数字字符0次或多次

代码:

with tmp1 as
(
select 'abc12' str from dual
union 
select 'abc123' str from dual  -- OK 
union 
select 'abc123ab' str from dual  -- OK 
union 
select '123abc' str from dual   -- OK 
union
select '12abc123' str from dual  -- OK 
union 
select '12abc1234' str from dual
union 
select '1234abc12' str from dual
union
select '123' str from dual      -- OK
)
select *
  from tmp1
 where str regexp '[^0-9]{1,}+[0-9]{3}+[^0-9]{1,}'   -- 匹配 %123%
    or str regexp '[^0-9]{1,}+[0-9]{3}$'             -- 匹配 %123
    or str regexp '^[0-9]{3}+[^0-9]{1,}'             -- 匹配 123%
    or str regexp '^[0-9]{3}$'                       -- 匹配 123

测试记录:

mysql> with tmp1 as
    -> (
    -> select 'abc12' str from dual
    -> union
    -> select 'abc123' str from dual  -- OK
    -> union
    -> select 'abc123ab' str from dual  -- OK
    -> union
    -> select '123abc' str from dual   -- OK
    -> union
    -> select '12abc123' str from dual  -- OK
    -> union
    -> select '12abc1234' str from dual
    -> union
    -> select '1234abc12' str from dual
    -> union
    -> select '123' str from dual      -- OK
    -> )
    -> select *
    ->   from tmp1
    ->  where str regexp '[^0-9]{1,}+[0-9]{3}+[^0-9]{1,}'   -- 匹配 %123%
    ->     or str regexp '[^0-9]{1,}+[0-9]{3}$'             -- 匹配 %123
    ->     or str regexp '^[0-9]{3}+[^0-9]{1,}'             -- 匹配 123%
    ->     or str regexp '^[0-9]{3}$'                       -- 匹配 123
    -> ;
+----------+
| str      |
+----------+
| abc123   |
| abc123ab |
| 123abc   |
| 12abc123 |
| 123      |
+----------+
5 rows in set (0.00 sec)

mysql>

参考:

  1. https://www.jb51.net/article/72928.htm
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值