Home > Web Front-end > JS Tutorial > How to match consecutive numbers using regular expressions

How to match consecutive numbers using regular expressions

php中世界最好的语言
Release: 2018-03-29 17:57:54
Original
9827 people have browsed it

这次给大家带来正则怎么匹配连续数字,正则匹配连续数字的注意事项有哪些,下面就是实战案例,一起来看一下。

实现的要求如下:

  • 纯数字

  • 5-7位之间

  • 前三位相同

  • 从第四位开始连续

如下面的例子:

11123    #正确
22234    #正确
33345    #正确
333456   #正确
2223456   #正确
0001234   #正确
00012345  #错误:此行长度超标
000234   #错误:第3位与第4位不连续
111235   #错误:第5位和第6位不连续
Copy after login

1、匹配三位相同数字

(\d)\1{2}
Copy after login

2、匹配连续数字

匹配连续数字可以通过零宽断言来匹配,当然也没什么好办法,只能通过列举,如下两行都可以匹配3位连续数字

(0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){2}\d
\d((?<=0)1|(?<=1)2|(?<=2)3|(?<=3)4|(?<=4)5|(?<=5)6|(?<=6)7|(?<=7)8|(?<=8)9){2}
Copy after login

说明: 第一行采用正向零宽断言,第二行采用反向零宽断言

结论

集合本题中的要求,由于要求相同前三,并且后面连续从第三位开始,完成正则如下:

^(\d)\1{2}((?<=(0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)))\d){2,4}$
^(\d)\1{2}((?<=0)1|(?<=1)2|(?<=2)3|(?<=3)4|(?<=4)5|(?<=5)6|(?<=6)7|(?<=7)8|(?<=8)9){2,4}$
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

使用正则表达式提取字符串详解(附代码)

容易产生错误的js手机号码验证

The above is the detailed content of How to match consecutive numbers using regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template