Php Regular Expression repeated characters

后端 未结 2 1014
名媛妹妹
名媛妹妹 2020-12-17 21:08

I have a string in php like this.

$str = \"ABCCCDE\" //Contains repeated character CCC more than 2 times

I want to know if there is any repeated characters

相关标签:
2条回答
  • 2020-12-17 21:42
    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. So this RegEx will match 3 same consecutive characters.

    0 讨论(0)
  • 2020-12-17 21:59

    You can use:

    '/(.)\1\1/'
    

    E.g.:

    preg_match('/(.)\1\1/', $str, $matches);
    
    0 讨论(0)
提交回复
热议问题