What is the regex to match a string not containing more than x consecutive characters

前端 未结 3 1811
栀梦
栀梦 2021-01-26 13:37

I want to match strings that do not contain more than 3 of the same character repeated in a row. So:

  • abaaaa [no match]
  • abawdasd [match]
  • abbbbasda
相关标签:
3条回答
  • 2021-01-26 14:19

    Use a negative lookahead with back references:

    ^(?:(.)(?!\1\1))*$
    

    See live demo using your examples.

    (.) captures each character in group 1 and the negative look ahead asserts that the next 2 chars are not repeats of the captured character.

    0 讨论(0)
  • 2021-01-26 14:19

    To match strings not containing a character repeated more than 3 times consecutively:

    ^((.)\2?(?!\2\2))+$

    How it works:

    ^            Start of string
    (
      (.)        Match any character (not a new line) and store it for back reference.
        \2?      Optionally match one more exact copies of that character.
        (?!      Make sure the upcoming character(s) is/are not the same character.
          \2\2   Repeat '\2' for as many times as you need
        )
    )+           Do ad nauseam
    $            End of string
    

    So, the number of /2 in your whole expression will be the number of times you allow a character to be repeated consecutively, any more and you won't get a match.

    E.g.

    • ^((.)\2?(?!\2\2\2))+$ will match all strings that don't repeat a character more than 4 times in a row.

    • ^((.)\2?(?!\2\2\2\2))+$ will match all strings that don't repeat a character more than 5 times in a row.

    Please be aware this solution uses negative lookahead, but not all not all regex flavors support it.

    0 讨论(0)
  • 2021-01-26 14:29

    I'm answering this question :

    Is there a regular expression for matching a string that has no more than 2 repeating characters?

    which was marked as an exact duplicate of this question.


    Its much quicker to negate the match instead

    if (!Regex.Match("hello world", @"(.)\1{2}").Success) Console.WriteLine("No dups");
    
    0 讨论(0)
提交回复
热议问题