XML technologies / Regular expressions / Practical regex techniques / HTML examples / Checking a hostname for syntactic validity
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
In the previous example, we used only ˹[^/:]+˼ for the server name, whereas in Extended introductory examples (see A regex collection) we used the more complicated ˹[-a-z]+(\.[-a-z]+)*\.(com|edu|...|info)˼. Why such different levels of effort for the same result?
Both subexpressions “match a hostname”, but in very different contexts. Extracting a part from a string with a known structure, for example when we know it is a URL, is relatively easy. Reliably and unambiguously identifying the same part in an arbitrary string is much harder.
In the previous example, we assumed that a hostname followed ›http://‹, so the simple ˹[^/:]+˼ was appropriate. But in the example from Extended introductory examples we had to develop a regex to recognize hostnames in arbitrary text, so we needed to be more specific.
Now, for a third purpose, we want a regular expression to check exactly whether a hostname is well formed and syntactically valid. Formally, an Internet hostname consists of several parts separated by dots. Each part consists of ASCII letters, digits and hyphens. A hyphen may not be the first or last character. Ignoring case, we can match one hostname part with ˹[a-z0-9]|[a-z0-9][-a-z0-9]*[a-z0-9]˼. The final part (›com‹, ›edu‹, ›de‹ and so on) has only a limited set of possibilities, as mentioned in passing in Extended introductory examples. This gives us the following regular expression for a syntactically valid hostname:
^
(?i) # Groß- und Kleinschreibung nicht beachten.
# Ein oder mehrere Teile, durch Punkte getrennt ...
(?: [a-z0-9]\. | [a-z0-9][-a-z0-9]*[a-z0-9]\. )+
# Top-level-Domains ...
(?: com|edu|gov|int|mil|net|org|biz|info|name|museum|coop|aero|[a-z][a-z] )
$A match with this regex is not necessarily valid yet. Individual parts also have a length limit: no more than 63 characters. We therefore replace the star in ˹[-a-z0-9]*˼ with {0,61}.
For complete formal correctness, there is another consideration. Even a name consisting only of the top-level domain (such as ›com‹, ›edu‹ and so on) is syntactically valid in itself. In practice, such hosts do not exist for the named domains; only some two-letter country domains have a responding host. Anguilla, for example, has a web server at "http://ai/", and web servers can also be found at the domains cc, co, dk, mm, ph, tj, tv and tw .
To allow this too, we replace the plus quantifier on the middle part ˹(?:...)+˼ with ˹(?:...)*˼, giving:
^
(?i) # Groß- und Kleinschreibung nicht beachten.
# Ein oder mehrere Teile, durch Punkte getrennt ...
(?: [a-z0-9]\. | [a-z0-9][-a-z0-9]{0,61}[a-z0-9]\. )*
# Top-level-Domains ...
(?: com|edu|gov|int|mil|net|org|biz|info|name|museum|coop|aero|[a-z][a-z] )
$This now works excellently for a string consisting only of a hostname. Since it is the most precise of our three hostname expressions, we might be tempted to remove the anchors at either end too. That would be a mistake: this regex matches any two-letter word. In that respect, even the less specific regex from Extended introductory examples is better. Depending on the context and intended use, however, this regular expression may still be insufficient.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln
back to top