Regular expression for validating hostname Cam2camfreechat

Posted by / 13-Jun-2016 00:39

Regular expression for validating hostname

I have recently had cause to find regular expressions that allow me to determine if some user input is an IP address, IP address range (in CIDR notation) or a hostname. d))))|:))|(:(((:[0-9A-Fa-f]))|((:[0-9A-Fa-f]):((25[0-5]|2[0-4]d|1dd|[1-9]?

The IP address and IP address range validators were needed for both IPv4 and IPv6. d))))|:))|(([0-9A-Fa-f]:)(((:[0-9A-Fa-f]))|((:[0-9A-Fa-f]):((25[0-5]|2[0-4]d|1dd|[1-9]?

A regex may be able to validate that a hostname conforms to one of the RFCs but it can't validate that the hostname is valid or that it exists or that it resolves to an IP address that runs an NTP server.

A regex is a bad way to achieve what I think you are trying to achieve, although you haven't actually stated what you are trying to achieve. A better way would be to perform a DNS lookup on the hostname and verify that it resolves to an IP address (be that IPv6 or IPv4) and that when you connect to that IP address on port 123, you get a valid NTP protocol response.

I found various bits and bobs around the web, and if I’m honest, I can’t remember where I got them from (please let me know if I owe you some attribution! But I don’t want to go looking for them again, and I think they will be useful to the wider community, so here they are. d))))|:))|(([0-9A-Fa-f]:)(((:[0-9A-Fa-f]))|((:[0-9A-Fa-f]):((25[0-5]|2[0-4]d|1dd|[1-9]?

There is no difference between a web server hostname, an email server hostname and an NTP server hostname.] [Updated: 6/Apr/2015 – fixed to resolve the problem pointed out by Pirabarlen in the comments] ^(([0-9]|[1-9][0-9]|1[0-9]|2[0-4][0-9]|25[0-5])\.)([0-9]|[1-9][0-9]|1[0-9]|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$ ^s*((([0-9A-Fa-f]:)([0-9A-Fa-f]|:))|(([0-9A-Fa-f]:)(:[0-9A-Fa-f]|((25[0-5]|2[0-4]d|1dd|[1-9]? d)))|:))|(([0-9A-Fa-f]:)(((:[0-9A-Fa-f]))|:((25[0-5]|2[0-4]d|1dd|[1-9]? s* [Updated: 6/Apr/2015 – fixed to resolve the problem pointed out by Pirabarlen in the comments] ^s*((([0-9A-Fa-f]:)([0-9A-Fa-f]|:))|(([0-9A-Fa-f]:)(:[0-9A-Fa-f]|((25[0-5]|2[0-4]d|1dd|[1-9]? d)))|:))|(([0-9A-Fa-f]:)(((:[0-9A-Fa-f]))|:((25[0-5]|2[0-4]d|1dd|[1-9]? Note that the backslash at the end of the first line (above) is Unix shell syntax for splitting the long line. Here's just the regular expression alone on a single line: I don't seem to be able to edit the top post, so I'll add my answer here.For hostname - easy answer, on egrep example here -- // the case doesn't account for values like 0 in the fist octet, and values greater than 254 (ip addres) or 255 (netmask). As for legal dns hostname, provided that you are checking for internet hostnames only (and not intranet), I wrote the following snipped, a mix of shell/php but it should be applicable as any regular expression.

regular expression for validating hostname-40regular expression for validating hostname-63regular expression for validating hostname-82

Note that the backslash at the end of the first line (above) is Unix shell syntax for splitting the long line. Here’s just the regular expression alone on a single line: This entry was posted on 2017/08/29 at and is filed under Development, Reg Ex, Software Development.

One thought on “regular expression for validating hostname”