Home > Articles > Programming

  • Print
  • + Share This
This chapter is from the book

IP Addresses

IP addresses are made up of four bytes (each with a valid range of 0-255). IP addresses are usually formatted as four sets of digits (each is one to three digits in length) separated by . characters.

localhost is 127.0.0.1.
(((\ d{ 1,2} )|(1\ d{ 2} )|(2[0-4]\ d)|(25[0-5]))\ .){ 3} ((\ d{ 1,2} )|(1\ d{ 2} )|(2[0-4]\ d)|(25[0-5]))
localhost is 127.0.0.1.

This pattern uses a series of nested subexpressions. The first is (((\ d{ 1,2} )|(1\ d{ 2} )|(2[0-4]\ d)|(25[0-5]))\ .), a set of four nested subexpressions. (\ d{ 1,2} ) matches any one- or two-digit number, or numbers 0 through 99. (1\ d{ 2} ) matches any three-digit number starting with 1 (1 followed by any 2 digits), or numbers 100 through 199. (2[0-4]\ d) matches numbers 200 through 249. (25[0-5]) matches numbers 250 through 255. Each of these subexpressions is enclosed within another subexpression with an | between each (so that one of the four subexpressions has to match, but not all). After the range of numbers comes \ . to match ., and then the entire series is enclosed into yet another subexpression and repeated three times using { 3}. Finally, the range of numbers is repeated (this time without the trailing \ .) to match the final IP address number. The pattern thus validates the format of the string to be matched (that it is four sets of numbers separated by .) and validates that each of the numbers has a value between 0 and 255.

  • + Share This
  • 🔖 Save To Your Account