Regular Expressions 101

Community Patterns

Validate proper formating for a single short hostanme or DNS FQDN or IPv4 address

0

Regular Expression
PCRE (PHP <7.3)

/
^((?=[0-9.]+$)((25[0-5]|(2[0-4]|1[0-9])[0-9]|[1-9]?[0-9])\.){3}(25[0-5]|(2[0-4]|1[0-9])[0-9]|[1-9]?[0-9])|(?=[0-9.]*[a-zA-Z])([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9])((\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]))*(\.[a-zA-Z]{2,31}))+)$
/
gm

Description

Accepts:

Valid IPv4:

127.0.0.1 192.168.1.1 192.168.1.255 192.168.255.255 0.0.0.0

Valid Hostnames:

abdc N7

Valid FQDN:

ABCD.fr truc-bidule.com sub.one-stuff.somewhere.com sub1.sub2.sub3.sub4.sub5.sub6.net

Rejects all the rest:

127.0.0.1 192.168.1.1a 192.068.1.255 30.168.1.255.1 127.1 192.168.1.256 -1.2.3.4 1.1.1.1. 3...3

523 rêve abc.f abc.c6 -n.fr abc.org a bc.fr ab. net to to to_to my_thing.com

Submitted by anonymous - 4 years ago