Regular Expressions in Java

Download Solution: Click to Download Solution
Solution File Name: SrcCode2CC.zip
Unzip Password: prestobear.com

Match IPv4 Addresses (50 points total)
Work on the following tasks in the order they are given. The order is designed to help you
incrementally build up a complete regex.
1. For this part you will complete the IPV4_ADDRESS_REGEX static variable in
class InternetAddressValidator. You will write your tests in
the TestIsIPv4Address JUnit class.
2. (30 points) Write, and thoroughly test, a regular expression that handles IPv4 addresses, e.g.,
“160.010.025.006”. For now, handle IP addresses where:
1. All octets are exactly 3 digits (i.e., octets in the range of 0-99 are left-padded with
zeroes).
2. There is no checking of invalid IP addresses, i.e., addresses like “500.400.123.456” will
pass.
3. (15 points) Modify your IPv4 regex so that it allows 1 – and 2-digit octets. E.g., “192.168.1.15”
will now match. Make sure you add unit tests to handle these cases.
4. (5 points) Modify your IPv4 regex so that it only allows valid IP addreses, i.e., those with
octets in the 0-255 range. Make sure you add unit tests to handle these cases.

Match IPv4 Subnet Addresses (40 points total)
Again, work on the following tasks in the order they are given.
1. For this part you will complete the SUBNET_ADDRESS_REGEX static variable in
class InternetAddressValidator. You will write your tests in
the TestIsSubnetAddress JUnit class.
2. (15 points) Write, and thoroughly test, a regular expression that handles simple subnet
addresses, e.g.,
1. 255.255.255.255
2. 255.255.255.0
3. 255.255.0.0
4. 255.0.0.0
5. 0.0.0.0
3. (10 points) Modify your regex so that it handles the “1 -0” break in the fourth octet, meaning it
handles addresses like:
1. 255.255.255.128
2. 255.255.255.192
3. 255.255.255.224
4. etc
Hint: use to your advantage the fact that this last octet of numbers will be one of: 0, 128,
192, 224, 240, 248, 252, 254, or 255 — “OR” is your friend here!
Make sure you test this part thoroughly.
4. (10 points) Modify and test your regex so that the “1 -0” break is handled in the 3rd octet as
well as the fourth. Don’t be afraid to add a special “third octet” case here — it might be simpler
than trying to “fix” what you already have.
5. (5 points) Modify and test your regex so that it handles the “1 -0” break in the first and second
octets.

Match IPv6 Addresses (10 points total)
Again, work on the following tasks in the order they are given.
1. For this part you will complete the IPV6_ADDRESS_REGEX static variable in
class InternetAddressValidator. You will write your tests in
the TestIsIPv6Address JUnit class.
2. (5 points) Write, and thoroughly test, a regular expression that handles full-length IPv6
addresses (i.e., those without the “double colons”). You may assume every field between
colons is exactly four hex digits (left-padded with zeros if necessary).
3. (2 points) Modify your regex so fields can have 1, 2, 3, or 4 hex digits (i.e., padding-withzeroes is not required, but still allowed). Write appropriate tests for these cases.
4. (1 point) Modify and test your regex so that it may have “double colons” at the beginning of an
IPv6 address (representing one or more fields of all-zeroes). Again, don’t be afraid to use
“OR” and add a special case for this!
5. (1 point) Modify and test your regex so that it may have “double colons” at the end of the IPv6
address.
6. (1 point) Modify and test your regex so that it may have “double colons” in the middle of an
IPv6 address.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.