Search code examples
pythonregexpyspark

Regular Expression - have at least n different digits


I want to use regular expression to check if the numbers have more than 2 different digits. For example, AB1000002 is allowed but AB1000000 is not allowed.

My question is similar to this one but seems to be more complicated. Reference: Regular Expression- have different digits

Thanks in advance!


I am not good at coding, I have tried to fix this problem but what I can do is just simply make the check of "at least 1 different digit" start from the second number...which is a bit stupid sorry I know... \d(\d)((?!\1)\d)+


Solution

  • Why to use regular expressions when the set() does it easy and efficiently?

    for st in ["AB1000002",  "AB1000000"]:
        if len(set(st[2:])) > 2: # remove AB and run set on numbers
            print(st)
    
    AB1000002