简体   繁体   English

Php正则表达式重复字符

[英]Php Regular Expression repeated characters

I have a string in php like this. 我有像这样的PHP字符串。

$str = "ABCCCDE" //Contains repeated character CCC more than 2 times $ str =“ABCCCDE”//包含重复的字符CCC超过2次

I want to know if there is any repeated characters more than 2 times using regular expression. 我想知道使用正则表达式是否有任何重复的字符超过2次。

Thanks in advance 提前致谢

if (preg_match('/(.)\\1{2}/', $str))
   echo "Has 3 same characters consecutively!";

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. (.)将匹配任何字符(新行除外), \\1将匹配与第一个匹配组相同的模式 - 在这种情况下,我们刚刚匹配的字符。 So this RegEx will match 3 same consecutive characters. 所以这个RegEx将匹配3个相同的连续字符。

You can use: 您可以使用:

'/(.)\1\1/'

Eg: 例如:

preg_match('/(.)\1\1/', $str, $matches);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM