How can I check a string contains at least 6 different digits using regular expressions?

I'm trying to get a regex that checks that a 9 character integer is long and contains at least 6 non-repeating digits

Example:

123456123 ------> Matches (6 different digits)
123243521 ------> Doesn't match (5 different digits)

+3


source to share


1 answer


It's much easier to do it without regex:



var str = "1234567890";
var isOk = str.Length >= 9
    && str.All(c => c >= '0' && c <= '9')
    && str.Distinct().Count() >= 6;

      

+10


source







All Articles