matching 3 or more of the same character in python

后端 未结 2 1095
予麋鹿
予麋鹿 2021-02-14 17:11

I\'m trying to use regular expressions to find three or more of the same character in a string. So for example: \'hello\' would not match \'ohhh\' would.

I\'ve tried doi

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-14 17:51

    if you're looking for the same character three times consecutively, you can do this:

    (\w)\1\1
    

    if you want to find the same character three times anywhere in the string, you need to put a dot and an asterisk between the parts of the expression above, like so:

    (\w).*\1.*\1
    

    The .* matches any number of any character, so this expression should match any string which has any single word character that appears three or more times, with any number of any characters in between them.

    Hope that helps.

提交回复
热议问题