Regular Expressions 101

IPv4 non zero first and last octet

Open Workspace
  • PCRE2 (PHP)
  • Match
0

Regular Expression

/
^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[1-9][0-9]?|0?[0-9]?[1-9])(.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])){2}.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[1-9][0-9]?|0?[0-9]?[1-9])$
/
gm

Description

When validating an IPv4 address I often have to reject devices that the first and last octet are 0.

Submitted by John Cutburth