Regular Expressions 101

Community Patterns

ipv4 and ipv6 with mask in CIDR

1

Regular Expression
PCRE2 (PHP >=7.3)

/
\b((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)+(?:\/{1}((?:[0-9])|(?:[1-2][0-9])|(?:3[0-2]))\b)?)|((?:[a-f0-9:]+:+)+(?:[a-f0-9])+(?:\/{1}((?:(?:3[0-2]|[12]?\d)))\b)?)
/
gm

Description

Match and validate both ipv4, ipv6 and CIDR notation mask!

Submitted by Stefanos Mitropoulos - 8 months ago