Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE (PHP <7.3)

/
^(?<ip_addr>((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|((([\da-fA-F]{1,4}:){7})([\da-fA-F]{1,4})$|(([\da-fA-F]{1,4}:){1,6}:)(([\da-fA-F]{1,4}:){0,4})([\da-fA-F]{1,4}))|(::[\da-fA-F]{1,4})|(([\da-fA-F]{1,4}:){0,7}[\da-fA-F]{1,4}::)))$
/
mg

Description

This is loosely based on some other examples, but goes a bit further in terms of what it will and won't match. In particular, it matches IPv6 addresses like ff02::, ff02:1000::, and (the syntactically valid but improbable) ::0001:0002

Submitted by Alan - 8 years ago