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

How can I prevent the use of more than two repeating digits in a string?

How to disallow the use of more than two repeating digits in a string?
I really don’t understand. I think I need to use backreferences

Valid:

122122122
949944494
5656665
10111001

Not valid:

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

122142122 <---- 4
949984494 <---- 8
5656265   <---- 2
10113001  <---- 3

>Solution :

You can capture the first character, match it with a backreference until you can capture a different second character, and then match the two captured characters with backrefereces in an alternation pattern until the end:

^(.)?\1*(.)?(?:\1|\2)*$

Demo: https://regex101.com/r/5wvoAp/4

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