Do not use regex to test a range of numbers.
Just use the condition ...
if ($number >= 0 AND $number <= 255) { ... }
This ensures that the number is between 0 and 255 inclusive, which is what your regular expression does.
To answer your question specifically, it does not work, because you need to wrap it all with a capture group, otherwise the regex engine will do the OR of each individual regex ...
/^([0-1]?[0-9]?[0-9]|[2][0-4][0-9]|25[0-5])$/
Also note that $ will match before any trailing \n . Use \z if you really want to match at the end of a line.
source share