RegEx IP Address Validation
Oct 28
RegEx IP Address Validation
I recently had a project at work where I had to add in some strict IP Address validation and restrictions. We are being required to prevent end users from entering certain IP addresses or ranges. This is a perfect task for Regular Expressions. I am not an expert in RegEx, but I can get out basic RegEx expressions. The first thing I did was search around the net to see if there were expressions already done for IP addresses. There are, but not entirely what I needed. The best I could find were expressions to validate the IP address to ensure it is valid. I didn't find anything to test for certain ranges of IP Addresses. Since I can hack at RegEx a bit I took the expression I found to validate the IP address and made modifications to do the tests I needed. These are the Regular Expressions I hacked up to do the validation and restrictions.
We were required to block these IP addresses or ranges.
2127.0.0.1
3255.255.255.255
410.0.0.0 - 10.255.255.255
5172.16.0.0 - 172.31.255.255
6192.168.0.0 - 192.168.255.255




