0

I'm sorry, but I am not sure how to correctly describe my problem. I have to match multiple lists of none-sequential 3-digit numbers, which must be prefixed by a string that includes another three digit number, and must end with a hash. Looking like this:

\*123\*(<list of 3 digit numbers>)#

This match can also have be optionally suffixed with another match of 3-5 digit numbers, looking like this:

\*123\*(<list of 3 digit numbers>)\*(<3-5 digit numbers>)#

I'm currently struggling on how to combine these two regexes without having to repeat the main list of none-sequential numbers (trying to avoid having to write this:

\*123\*((<list of 3 digits numbers>)|(<list of 3 digit numbers)\*\d{3-5}))# )

, so the entire regex is a bit more readable, and easy to modify.

The (<list of 3 digit numbers>) I'm trying to avoid having to write twice is very hard to read:

((10[1-9]|110)|2[0-9][09]|(30[1-9]|110)|(40[3-9])|41[01])|(41[2-6])|(42[12])|(43[0-9])|(44[1,5-9])|((?!456)45[0-9])|(46[1-7])|(47[2,5-9]|48[0-9]|49[4-9])||(51[2,3,6,8,9])|((?!526)52[0-9])|(53[0,2,5,7,9])|(54[0-1,3-9])|(55[0-4,6,9])|(56[0-6,8])|(57[0-2])

Please is what I intend, possible?

Thanks in advance!

Unpossible
  • 603
  • 6
  • 23
  • 1
    So, you just want to shorten the regex in code, without having to repeat its parts? Build the pattern from variables. – Wiktor Stribiżew May 29 '20 at 09:02
  • Thanks a lot, the duplicate question you led me to gave me some insight. I ended up with this: `\*123\*((?(3-digit-number match list)|(?&list_name)\*(\d{3,5}))#`. Now to ensure the application needing this doesn't eat the named group in some way... – Unpossible May 29 '20 at 10:54
  • Note it will only work with PyPi `regex` module if you plan to use it in Python. It won't work in Python `re`. Better build a pattern from variables, it is more portable across languages. – Wiktor Stribiżew May 29 '20 at 10:58
  • Ok. Thanks, I'll research that. Ive been testing so far here https://regex101.com/r/baLRGQ/2 – Unpossible May 29 '20 at 11:02
  • And you clearly see that with Python options set, [it does not work](https://regex101.com/r/baLRGQ/3) – Wiktor Stribiżew May 29 '20 at 11:08
  • Yes you are right. Not just that, but the application needing the regex processes named groups in a certain manner. Back to the drawing board! – Unpossible May 29 '20 at 11:13

0 Answers0