Regular Expressions 101

Community Library Entry

-1

Regular Expression
PCRE (PHP <7.3)

/
((?'ip4'(\d{1,3}\.){3}\d{1,3})|(?'ip6'(([0-9a-f]{1,4})?:){7}([0-9a-f]{4})?))
/
gm

Description

regex to capture ip4/ip6 addresses with named capture groups. The Following features are included:

  • PHP and python support
  • ip4 byte size check (ensures byte can't exceed 255)
  • ip6 checks if hex characters are valid
  • uses a negative lookbehind to check if the ip is a word on its own
  • named capture groups to facilitate extraction of either ip4 or ip6
Submitted by Meryo - 4 years ago