REGEX - Matching any character which repeats n times

前端 未结 4 1628
有刺的猬
有刺的猬 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:09

    Regular expressions (and finite automata) are not able to count to arbitrary integers. They can only count to a predefined integer and fortunately this is your case.

    Solving this problem is much easier if we first construct a nondeterministic finite automata (NFA) ad then convert it to regular expression.

    So the following automata for n=2 and input alphabet = {a,b,c,d}

    will match any string that has exactly 2 repetitions of any char. If no character has 2 repetitions (all chars appear less or more that two times) the string will not match.

    Converting it to regex should look like

    "^([^a]*a[^a]*a[^a]*)|([^b]*b[^b]*b[^b]*)|([^b]*c[^c]*c[^C]*)|([^d]*d[^d]*d[^d]*)$"
    

    This can get problematic if the input alphabet is big, so that regex should be shortened somehow, but I can't think of it right now.

提交回复
热议问题