Regular expression to mask a subnet?

I use regex to check for subnet masking. I use ajax txtbox with a mask value, but this does not work, then I switched to the text box and applied a regular expression for this. Unfortunately, it doesn’t work either.

Can you help me give a RE to mask the subnet 255.255.255.255

Or is there any better way to do this?

Decision

I used a masked text box and don't know how to put a validation expression.

Finally, I found the masked text field property as a validation expression, and there I put the RE and changed the validate property to true.

No need to explicitly use a validator expression.

thanks

+6
source share
6 answers

If you want to accept any IP address as a subnet mask:

var num = @"(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})"; var rx = new Regex("^" + num + @"\." + num + @"\." + num + @"\." + num + "$"); 

I thought it was easier to separate the "repeating" match for a particular group of numbers in a separate variable.

As an exercise for the reader, I will give another version of the expression. This one will capture all numbers in the same group, but different captures:

 var rx = new Regex("^(?:" + num + @"(?:\.(?!$)|$)){4}$"); 

BUT, but this is wrong, you should use this

 var num = @"(255|254|252|248|240|224|192|128|0+)"; var rx = new Regex("^" + num + @"\." + num + @"\." +num + @"\." +num + "$"); 

or

 var rx = new Regex("^(?:" + num + @"(?:\.(?!$)|$)){4}$"); 

http://www.freesoft.org/CIE/Course/Subnet/6.htm

+6
source share

To do this with regex, you need to make sure that the entire quad-core quad-core IPv4 is a 32-bit leading-only number. It is not enough to make sure that each number in the square has only leading ones. For example, 255.192.255.0 is not a valid subpattern, although each number in the square has only leading numbers. Based on the solution proposed by @xanatos,

var leadingOnes = new Regex("255|254|252|248|240|224|192|128|0+");

defines a regular expression that will match any 8-bit (decimal) number with leading only. I used "0+" to use .000, which is sometimes used on quads. Obviously, if you want to force a single zero, use "0" instead.

Then you need to create a regular expression that matches any of the four following patterns that I present as pseudo regular expressions to make it easier to understand:

  • 255.255.255. LeadingOnes
  • 255,255. LeadingOnes *. 0
  • 255. leadingOnes .0.0
  • leadingOnes .0.0.0

You can either write this as a single line, or create it by concatenating. Here it is being built:

 var leadingOnes = "(255|254|252|248|240|224|192|128|0+);" var allOnes = @"(255\.)"; var re = new Regex("^((" + allOnes + "{3}" + leadingOnes + ")|" + "(" + allOnes + "{2}" + leadingOnes + @"\.0+)|" + "(" + allOnes + leadingOnes + @"(\.0+){2})|" + "(" + leadingOnes + @"(\.0+){3}))$"); 

And here is the whole line if we ignore line breaks.

var re = new Regex(@"^(((255\.){3}(255|254|252|248|240|224|192|128|0+))|((255\.){2}(255|254|252|248|240|224|192|128|0+)\.0)|((255\.)(255|254|252|248|240|224|192|128|0+)(\.0+){2})|((255|254|252|248|240|224|192|128|0+)(\.0+){3}))$");

Following @Keith's suggestion, you can start with a simple regex like

Regex("([0-9]{1,3}\.){3}[0-9]{1,3}" to get four three-digit numbers separated by periods, and then write a function that extracts and evaluates four parts into a 32-bit integer, which you then check to make sure that it has only leadings.There are several ways to do this, but they all require up to 31 comparison operations to complete the test.

+11
source share

I know the question about regex expression, but for someone who cares, here are two iterative solutions to the problem. The second function is slightly faster than the first.

 private bool IsValidSubnet(IPAddress ip) { byte[] validOctets = new byte[] { 255, 254, 252, 248, 240, 224, 192, 128, 0 }; byte[] ipOctets = ip.GetAddressBytes(); bool restAreZeros = false; for (int i = 0; i < 4; i++) { if (!validOctets.Contains(ipOctets[i])) return false; if (restAreZeros && ipOctets[i] != 0) return false; if (ipOctets[i] < 255) restAreZeros = true; } return true; } // checks if the address is all leading ones followed by only zeroes private bool IsValidSubnet2(IPAddress ip) { byte[] ipOctets = ip.GetAddressBytes(); bool restAreOnes = false; for (int i = 3; i >= 0; i--) { for (int j = 0; j < 8; j++) { bool bitValue = (ipOctets[i] >> j & 1) == 1; if (restAreOnes && !bitValue) return false; restAreOnes = bitValue; } } return true; } 
+2
source share

From http://pastebin.com/wTEKjKpP

 var subnetRegex = /^((128|192|224|240|248|252|254)\.0\.0\.0)|(255\.(((0|128|192|224|240|248|252|254)\.0\.0)|(255\.(((0|128|192|224|240|248|252|254)\.0)|255\.(0|128|192|224|240|248|252|254)))))$/ 

Of course, this is for javascript, but this should help.

+2
source share

Description

It's a bit late for the party, but we can't perform regular expression validation on valid octets because:

  • The subnet should start at 255.XXX
  • The subnet cannot look like this: 255.254.128.X - after there will be 0 bits, everything else should be 0 after

The proper way to do this is to go through a bit from the MSB by checking the first bit 0 bits. Once you find the first bit 0, check what position it is in. The largest legal subnet is / 8 or 255.0.0.0, that is, there must be 8 1 bits before the first zero. Then make sure that every bit after the first 0 is zero. So in short:

  • (optionally check that it even has a valid IP address ...)

  • Start with MSB, go down, look for the first 0

  • If you find that 0 (255.255.255.255 will still be valid), check the position
  • Make sure all remaining bits are zero.

the code

  private bool IsValidSubnet(string subnet) { //A subnet is a valid ipv4 address, so start checking there if (!IsIPv4(subnet)) return false; // Get the 4 bytes byte[] subnetMaskBytes = System.Net.IPAddress.Parse(subnet).GetAddressBytes(); //Shift to get uint representation of the bits var UintSubnet = (uint)subnetMaskBytes[0] << 24; UintSubnet += (uint)subnetMaskBytes[1] << 16; UintSubnet += (uint)subnetMaskBytes[2] << 8; UintSubnet += (uint)subnetMaskBytes[3]; int i = 31; while (i >= 0) { UInt32 mask = (UInt32)(1 << i); if ((UintSubnet & mask) == 0) break; i--; } // It is not legal to have fewer than 8 bits of addressing if (i >= 24) return false; // Make sure that all remaining bits are 0 while (i >= 0) { UInt32 mask = (UInt32)(1 << i); if ((UintSubnet & mask) != 0) return false; i--; } return true; } 
0
source share

I tried @Joel Lee to answer an example. It works great with minor changes. In var leadingOnes = "(255 | 254 | 252 | 248 | 240 | 224 | 192 | 128 | 0+); - The semicolon must be outside the line. If it is inside, then it does not correctly count the subnet.

0
source share

All Articles