Regex to check ip range from ip list

I have a regex to check a list of 50 ips with commas:

^(?:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:,? ?)){1,50}$
List Example

:

10.10.10.1,127.0.0.1

Now I need to maintain a list, for example:

10.10.10.1,127.0.0.1-127.0.0.125

I'm trying to use subpatterns but nothing happened

can anyone provide a regex example for checking this line:

127.0.0.1-127.0.0.125
+4
source share
1 answer

Here you can use two things: use a negative character class to match either ,, or -, and use a subroutine call to make sure your template stays of a valid size and the line cannot start with ,or -:

^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:[,-](?1)){0,49}$

Watch the regex demo

More details

  • ^ -
  • ((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)) - 1, 1 IP-
  • (?:[,-](?1)){0,49} - 0 49 ( 1 IP, 50 ) :
    • [,-] - , ,, -
    • (?1) - , "" 1 ( , , )
  • $ -
+4

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


All Articles