I've made this regex to check whether a string is a valid IP, including checking if each octel is between 0-255 (more specifically, it checks if it's between 0-199 OR 200-249 OR 250-255)
((1?\d?\d|2[0-4]\d|25[0-5])\.){3}(1?\d?\d|2[0-4]\d|25[0-5])
A more simple way of looking at it is:
(pattern\.){3}pattern
which matches pattern.pattern.pattern.pattern
such that pattern is a number between 0 and 255.
I was wondering if there's a shorter way of writing this regex, such that pattern
only has to be written once, while not compromising the accuracy of the regex, like (pattern\.?){4}
does.
edit
Looks like this: ^pattern($|\.(?!$)){4}$
does exactly what I'm looking for.
Taken from: https://stackoverflow.com/a/28446132/4526483