Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match number with different digits and minimum length

I am trying to write a regex (to validate a property on a c# .NET Core model, which generates javascript expression) to match all numbers composed by at least two different digits and a minimum length of 6 digits.

For example:

222222 - not valid

122222 - valid

1111125 - valid

I was trying the following expression: (\d)+((?!\1)(\d)) , which matches the sequence if has different digits but how can I constrain the size of the whole pattern to {6,} ?

Many thanks

like image 986
user3042203 Avatar asked Nov 15 '25 23:11

user3042203


1 Answers

You may use

^(?=\d{6})(\d)\1*(?!\1)\d+$

See the regex demo

Details

  • ^ - start of string
  • (?=\d{6}) - at least 6 digits
  • (\d) - any digit is captured into Group 1
  • \1* - zero or more occurrences of the value captured in Group 1
  • (?!\1) - the next digit cannot be the same as in Group 1
  • \d+ - 1+digits
  • $ - end of string.
like image 93
Wiktor Stribiżew Avatar answered Nov 18 '25 12:11

Wiktor Stribiżew