# A Regular Expression for Validating An Internet Domain

DevFeed: [A Regular Expression for Validating An Internet Domain](<https://devfeed.tech/articles/a-regular-expression-for-validating-an-internet-domain-28296.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/regex/2020/02/03/a-regex-for-an-internet-domain.html>)

Author: Fuzzygroup

Published: 2020-02-03T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Regular expression](<https://devfeed.tech/topics/regular-expression.md>), [Internet](<https://devfeed.tech/topics/internet.md>)

Tags: [blog-post](<https://devfeed.tech/tags/blog-post.md>), [expression](<https://devfeed.tech/tags/expression.md>), [internet](<https://devfeed.tech/tags/internet.md>), [regex](<https://devfeed.tech/tags/regex.md>)

## AI overview

The article corrects an overly broad regular expression for validating Internet domain names. It explains that non-whitespace characters incorrectly allow underscores and presents a pattern using letters, digits, and hyphens in each domain label.

## Source excerpt

Despite the power and truth of Jamie Zawinski's law: Regular Expressions: Now You Have Two Problems Jeff Atwood's Perspective Like Jeff, I too really, really love regular expressions or regexes. I use this one a lot and I finally learned to use \S (Any non-whitespace character) so here's a regex ^\S+\.\S+$ that I wrote yesterday to "validate" the permitted characters in an Internet domain. I was all proud of this and wrote this blog post only to realize that pride really does goeth before a fall - this will NOT correctly validate an Internet domain. As I write this post, I realize that the number of allowed characters in an Internet domain are actually NOT any non-whitespace characters and here's the proof that I actually got that wrong yesterday when I put something online using it: Note: The fact that Rubular allows through an _ which is NOT a valid character in domains is problematic. So the right way to do this, DAMN IT, is something like this: ^[A-Za-z0-9\-]+\.[A-Za-z0-9\-]+$ And this actually works: The [A-Za-z0-9-]+ is a "character class" which says "Any uppercase or lowercase letter plus 0-9 plus a -" are allowed (any order, any quantity)". Regular Expressions - Now you have two problems.