REGEX - Matching any character which repeats n times

前端 未结 4 1622
有刺的猬
有刺的猬 2021-01-01 19:45

How to match any character which repeats n times?

Example:

for input: abcdbcdcdd
for n=1:   ..........
for n=2:    .........
for n=3:            


        
4条回答
  •  庸人自扰
    2021-01-01 20:07

    I would not use regular expressions for this. I would use a scripting language such as python. Try out this python function:

    alpha = 'abcdefghijklmnopqrstuvwxyz'
    def get_matched_chars(n, s):
        s = s.lower()
        return [char for char in alpha if s.count(char) == n]
    

    The function will return a list of characters, all of which appear in the string s exactly n times. Keep in mind that I only included letters in my alphabet. You can change alpha to represent anything that you want to get matched.

提交回复
热议问题