I need a regex pattern that validates IP addresses. This is easy enough to google for, but I there is a small catch. I need the last numbers to be able to accept a range. So the following input would validate.
XXX.XXX.XXX.X-Y
so something like 168.68.2.1-34
I have found patterns for normal IP addresses, but none for handling ranges.
To accept a number between 0 and 255, I’d use
(25[0-5]|2[0-4]\d|[0-1]?\d{1,2}). Calling the above expression A, a Regex for an IP Range would look likeor
If the range is optional, use
(-A)?instead of-A.This does not check if the start of the range is smaller than the end, but this cannot be done in Regex with reasonable effort.