PHP正则表达式重复字符 [英] Php Regular Expression repeated characters
本文介绍了PHP正则表达式重复字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在php中有这样的字符串.
I have a string in php like this.
$ str ="ABCCCDE"//包含重复字符CCC超过2次
$str = "ABCCCDE" //Contains repeated character CCC more than 2 times
我想知道使用正则表达式是否有超过2次的重复字符.
I want to know if there is any repeated characters more than 2 times using regular expression.
预先感谢
推荐答案
if (preg_match('/(.)\\1{2}/', $str))
echo "Has 3 same characters consecutively!";
(.)
将匹配任何字符(换行符除外),\1
将匹配与第一个匹配组相同的模式-在这种情况下,我们刚刚匹配的字符.因此,此RegEx将匹配3个相同的连续字符.
The (.)
will match any character (except new lines), and the \1
will match a pattern same as the first matched group — in this case, the character we've just matched. So this RegEx will match 3 same consecutive characters.
这篇关于PHP正则表达式重复字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文