Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading