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

Is there a way to write better regex when we have so much ORs

I have this following regex, starts with 09 and has a 2 digit number that should be only in some specific numbers, then 7 digits:

^[0][9](11|12|13|14|15|16|17|90|91|92|93|94|30|33|35|36|37|38|39|01|02|03|04|05|41|20|21|22|32|31|34|42)[0-9]{7}$

Or in python:

numbers = r"11|12|13|14|15|16|17|90|91|92|93|94|30|33|35|36|37|38|39|01|02|03|04|05|41|20|21|22|32|31|34|42"
phone_number_regex = rf"^[0][9]({numbers})[0-9]{7}$"

Is there a way to make this regex shorter? there is too much |s.

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

>Solution :

Looking at the alternatives, it seems the following matches the same things:

^09(0[1-5]|1[1-7]|2[012]|3[0-9]|4[12]|9[0-4])[0-9]{7}$
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