3

I have gone through this wonderfull post on stackoverflow

Validate IP address is not 0.0.0.0 or multicast address

But this is a custom javascript function which i have to write in my aspx page, instead of this is there any regex equivalent which could be helpful to validate the multicast ip-address

e.g. multicast IP address between 224.0.0.1 and 239.255.255.255

Thanks in advance for your efforts. :)

Community
  • 1
  • 1
Manish Rawat
  • 1,142
  • 1
  • 18
  • 34

2 Answers2

14

Personally I'd use math, but if you really want a regex,

^2(?:2[4-9]|3\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d?|0)){3}$
 <---|------|-->        <-----> <------> <---> <------> |  |
     |      |           |       |        |     |        |  three more octets
     |      |           |       |        |     |        |
     |      |           |       |        |     |        0
     |      |           |       |        |     |
     |      |           |       |        |     1 - 99
     |      |           |       |        |
     |      |           |       |        100 - 199
     |      |           |       |
     |      |           |       200 - 249
     |      |           |
     |      |           250 - 255
     |      |
     |      230 - 239
     |
     224 - 229
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
  • ahmm, thats really stunning regex for me, i would really thankfull to you to put your time on this. let me check and validate whether this regex works or not. +1 to you. :) – Manish Rawat Oct 31 '12 at 05:39
  • 1
    This answer is extremely close, but 224.123.123.256 will match it. It needs a $ at the end. So it should be 2(?:2[4-9]|3\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d?|0)){3}$ – scheibk Jun 15 '15 at 19:47
1

Best solution will be to port javascript code from here: Validate IP address is not 0.0.0.0 or multicast address to c#. Regular expressions are not suited to check ranges of numbers. You can write regular expression that covers all the possible combinations of numbers, but result will be huge and ineffective.

Community
  • 1
  • 1
Anri
  • 6,175
  • 3
  • 37
  • 61