Regex(Dataannotation) for digits not repeating more than five times in string

Viewed 47

I am just looking forward to create a regex which is not having consecutive digits repeating more than five times as well as it should start from only 6,7,8,9 digits.

I have solution but I'm using below 2 regex and validating.

string startPattern = "^[6-9][0-9]{9}$";
string endPattern = "([\\d])\\1{5}";

Please help, so that I can use as dataannotations in model.

1 Answers

You may use

^(?!.*(\d)\1{5})[6-9]\d{9}$

See the regex demo

Details

  • ^ - start of string
  • (?!.*(\d)\1{5}) - a negative lookahead that fails the match if there are identical 6 digits after any 0+ chars other than newline chars
  • [6-9] - 6 to 9 digit and then
  • \d{9} - nine digits
  • $ - and end of string.
Related