正则表达式:如何匹配数字? [英] Regular expressions: how to match numbers?

查看:615
本文介绍了正则表达式:如何匹配数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用正则表达式来匹配这样的数字:

I want to use regular expressions to match numbers like these:

58158
60360
98198

其格式为ABCAB.

我使用下面的代码来匹配ABAB:

I use code below to match ABAB:

(([\d]){1,}([\d]){1,})\1{1,}

(例如5858),但如何匹配ABCAB(58158)?

such as 5858 but how to match ABCAB(58158)?

推荐答案

对于格式为ABCAB的数字:

(\d)(\d)\d\1\2

这对A=B=C没有任何限制.对A!=B!=C使用否定前瞻:

This places no restriction on A=B=C. Use negative look-ahead for A!=B!=C:

(\d)(?!\1)(\d)(?!\1|\2)\d\1\2

没有边界匹配,因此58158将在36958158中匹配:

There is no boundary matching so 58158 will be matched in 36958158:

$num=36958158;
preg_match('/(\d)(?!\1)(\d)(?!\1|\2)\d\1\2/',$num,$match);
echo ">>> ".$match[0];

>>> 58158

这篇关于正则表达式:如何匹配数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆