Is there a way to match a range of numbers(0-255) without the ^
and $
?
Matched numbers
- 1
- 12
- 123
Not matched numbers
- 1234
- 555
Is there a way to match a range of numbers(0-255) without the ^
and $
?
You can use lookahead and lookbehind to match only 1-3 digits.
(?<!\d)(?:[1-9]?\d|1\d\d|2(?:[0-4]\d|5[0-5]))(?!\d)
Not using anchor characters, BrightOne was wise to integrate lookarounds for numeric characters. However, the pattern isn't fully refined. To optimize the pattern for speed and maintain accuracy:
This is my suggested pattern: (Demo)
/(?<!\d)(?:1\d{2}|2[0-4]\d|[1-9]?\d|25[0-5])(?!\d)/ #3526 steps
(Brightone's pattern resolves in 5155 steps)
(treesongs' second pattern resolves in 5184 steps) *at time of posting, the first pattern was inaccurate)