1

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.

JvdV
  • 70,606
  • 8
  • 39
  • 70
Mojtaba Arezoomand
  • 2,140
  • 8
  • 23

2 Answers2

4

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}$
JvdV
  • 70,606
  • 8
  • 39
  • 70
  • If numbers in this list have business meaning (like allowed area codes in phone number), then compression like this completely hides it. Yes, the regex is shorter, but at what cost? :) – Sergio Tulentsev Jan 13 '22 at 15:12
  • 1
    Depending on your personal experience, this form of writing alternatives is perfectly readable @SergioTulentsev. Either way, the question was if it could be written "shorter". – JvdV Jan 13 '22 at 15:14
  • @JvdV: yeah, I'm just thinking out loud. Your version is not identical to the original expression, btw. It rejects 20, for example. Maybe others too. My point was: the list may be long, but it's nearly impossible to mess it up. Unlike this scheme. Especially when one needs to add/remove numbers. – Sergio Tulentsev Jan 13 '22 at 15:16
  • @SergioTulentsev, good point indeed. I missed the 20. Will edit now. – JvdV Jan 13 '22 at 15:16
  • 1
    Anyway, good stuff. +1 – Sergio Tulentsev Jan 13 '22 at 15:20
  • This is what I have used: `^09(0[1-5]|1[1-8]|2[0-2]|3[0-9]|4[12]|9[0-4])[0-9]{7}$`. Thank you, it helped so much – Mojtaba Arezoomand Jan 13 '22 at 15:25
  • 1
    You could also change the `[0-9]{7}` to `\d{7}`. – mrCarnivore Jan 13 '22 at 16:29
1

I'm on mobile so I can't do the string split and sort to get the full thing, but what you can do is use character ranges to handle each group.

11|12|13|14|15|16|17|...

becomes

1[1-7]|...

and so on.

samthecodingman
  • 23,122
  • 4
  • 30
  • 54