Regular Expressions 101

Community Patterns

IPv4 and MAC addresses

0

Regular Expression
PCRE (PHP <7.3)

/
\b( # MAC address like xx:xx:xx:xx:xx:xx [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2} | # MAC address like xx-xx-xx-xx-xx-xx [0-9a-f]{2}-[0-9a-f]{2}-[0-9a-f]{2}-[0-9a-f]{2}-[0-9a-f]{2}-[0-9a-f]{2} | # MAC address like xxxx.xxxx.xxxx [0-9a-f]{4}\.[0-9a-f]{4}\.[0-9a-f]{4} | # IPv4 address in dotted-quad format # Not a lot of value for our needs in checking range of each octet # Need to be more mindful of performance here anyway. \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} )\b
/
ix

Description

Match tokens that are either IPv4 addresses or MAC addresses (three formats)

Submitted by anonymous - 4 years ago