Regex question, a range of numbers between two delimiters | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Regex question, a range of numbers between two delimiters

How do I fix this regular expression so that it finds all running numbers from 0 to 255 in the middle of # and ; ? #0; = True #255; = True #256; = False #abc; = False #-1; = False I tried this but it doesn't work: #[0-255]; but nothing. Regex101: https://regex101.com/

29th Jan 2022, 9:54 AM
Mick
Mick - avatar
8 ответов
+ 3
I have experimented a little bit and have come up with the following expression #([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5]);
29th Jan 2022, 10:17 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
this is not beautiful. but it works. /#\b([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5]);/gm https://regex101.com/r/jEBRVW/3 how to break down number range for regex: https://www.regextutorial.org/regex-for-numbers-and-ranges.php
30th Jan 2022, 3:18 AM
Bob_Li
Bob_Li - avatar
+ 2
devanille Bob_Li Thanks for the explanation, really helpful
30th Jan 2022, 10:45 AM
Mick
Mick - avatar
+ 1
So I tried both and both meet the requirements, thank you both, I will check the first who answered me, I would like to put them both, thank you very much to both Ani Jona 🕊 ravilnicki , regexes are as useful as they are complicated!
29th Jan 2022, 10:37 AM
Mick
Mick - avatar
0
That is unfortunately not quite that simple. You will need to check individually in the range of 250 to 255, the range of 200 to 249, as well as the range of 0 to 199. This may further be complicated whether or not a leading zero is allowed in the numbers.
29th Jan 2022, 10:05 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
0
Ani Jona 🕊 The leading 0 like 025 for example can be fine, so I explicitly convert it into a number later. Do you know how I can check the ranges described by you ?
29th Jan 2022, 10:08 AM
Mick
Mick - avatar
0
I don't understand, why is it not correct?
29th Jan 2022, 10:39 AM
Mick
Mick - avatar
0
Ok
29th Jan 2022, 10:43 AM
Mick
Mick - avatar