0

I have regex for IPv4 address:

^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$

and i have regex for IPv4 CIDR range :

^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(/([0-9]|[1-2][0-9]|3[0-2]))$

the issue is how should i repeat it using comma separated

pattern:

XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXX/XX, XX.XX.XX.XX, XX.XX.XX.XX/X , XX.XX.XX.X test data--

123.123.13.11, 1.0.0.0, 1.0.0.1/3, 1.0.0.0/20

am using http://regexr.com/ to build by regex, the regex which i build is below and not working--

/(((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))),?)/g
user7353226
  • 55
  • 2
  • 8

2 Answers2

1

Is this what you're looking for?

/((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/(3[0-1]|2[0-9]|1[0-9]|[4-9]))\,?\b){1,}/g

Edit: Breakdown

Match an IP address:

   (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.
   (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.
   (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.
   (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

(\/(30|2[0-9]|1[0-9]|[4-9]))? / followed by a number between 4 and 31.

\,? Comma. Optional.

? Space. Optional.

\b End of word.

){1,} End of capturing group. All at least once.

juanferrer
  • 1,192
  • 1
  • 16
  • 29
  • but it did not match the subnet. after slash(/) only digit should be allowed. – user7353226 Apr 18 '17 at 17:18
  • You're right. Modified it to support subnet mask. Is it what you were looking for? – juanferrer Apr 18 '17 at 17:38
  • sample not validating 151.140.0.0/16,165.130.0.0/16,50.207.27.182/32,192.8.0.0/16,207.11.0.0/17,50.202.130.24/29 – user7353226 Apr 18 '17 at 17:57
  • Regarding the first string, you should only have 30 hosts on each subnet. Anyways, it would be very simple to allow it to go up to 32, just substitute 30 with 3[0-2] On the other hand, you will need a second check for that unless you're assuming every IP address will come with a subnet. – juanferrer Apr 18 '17 at 18:25
1

To loop surround regex with ()* ex (<regex>)* if matching start and end then move terminators out of loop like ^(regex)*$

To match , or end-of-line append ([,\s]+|$) exclude \s if you don't want whitespace, + means match one or more.

This should work for you to match the whole string. Remove * at end for valid parts; surround with ^ $ to match full string.

IPV4 = (([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
Optional subnet = (\/([4-9]|[12][0-9]|3[0-2]))?
coma or end of line = (,|$)
Putting it together = (((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([4-9]|[12][0-9]|3[0-2]))?)([,\s]+|$))*

Or, for minimal group matching ((?!\\/) is negative look ahead for /, not all regex engines support negative look ahead)

(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/(3[0-1]|2[0-9]|1[0-9]|[4-9]))?(?!\/)\b
Tezra
  • 8,463
  • 3
  • 31
  • 68