Regular Expressions 101

Community Patterns

Publicly addressable IP addresses, excluding private ranges.

0

Regular Expression
PCRE (PHP <7.3)

/
^(?!(192\.168\.))^(?!(172\.(1[6-9]|2[0-9]|3[0-1])\.))^((([1-9]|1[1-9]|[2-9][0-9]|1[0-9]{2}|2[0-1][0-9]|22[0-3])\.)((0|1[0-9]{0,2}|[2-9][0-9]|2[0-4][0-9]|25[0-5])\.){2}(1[0-9]{0,2}|[2-9][0-9]|2[0-4][0-9]|25[0-4]))$
/
gm

Description

Valid IP Octets include: 1-9 & 11-223, then 0-255, twice, then 1-254 but with no leading zeroes.

This will exclude the 10.0.0/8 private IP range, the reserved and multi-cast range, 224.0.0.0 and up, as well as the private IP ranges 172.16.0.0/12 and 192.168.0.0/16.

In other words, this will address all publicly accessible IPv4 addresses from 1.0.0.1 through 223.255.255.254.

Submitted by Schvenn - 8 years ago