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

梦想与她 提交于 2020-01-25 08:16:47

问题


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 [no match]
  • bbabbabba [match]

Yes, it would be much easier and neater to do a regex match for containing the consecutive characters, and then negate that in the code afterwards. However, in this case that is not possible.

I would like to open out the question to x consecutive characters so that it can be extended to the general case to make the question and answer more useful.

Negative lookahead is supported in this case.


回答1:


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.




回答2:


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");



回答3:


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.



来源:https://stackoverflow.com/questions/59275616/is-there-a-regular-expression-for-matching-a-string-that-has-no-more-than-2-repe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!