Regex for checking IP addresses with a range?

I need a regex pattern that validates IP addresses. It's easy enough for Google, but I have a little catch. I need the latest numbers to be able to accept the range. Thus, the next entry will be confirmed.

XXX.XXX.XXX.XY

so something like 168.68.2.1-34

I found patterns for regular IPs, but not for range processing.

+3
source share
3 answers

To accept a number from 0 to 255, I would use (25[0-5]|2[0-4]\d|[0-1]?\d{1,2}). Calling the above expression A, Regex for the IP range will look like

A\.A\.A\.A-A

or

(A\.){3}A-A

If a range is optional, use (-A)?instead -A.

, , , Regex .

+4

, :

(normal-ip-regex)-[0-9]+

- , , (, , "X" , "Y", ).

+3

"" IP- . , - , .

, , - inet_aton ( ).

Once you have the address as an integer, you can verify that it is within the valid range, just as you check any other integer (<and> most likely).

+2
source

Source: https://habr.com/ru/post/1751138/


All Articles