## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# 8.15. Validating Domain Names

## Problem

You want to check whether a string looks like it may be a valid, fully qualified domain name, or find such domain names in longer text.

## Solution

Check whether a string looks like a valid domain name:

`^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}\$`
 Regex options: Case insensitive Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python
`\A([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}\Z`
 Regex options: Case insensitive Regex flavors: .NET, Java, PCRE, Perl, Python, Ruby

Find valid domain names in longer text:

`\b([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}\b`
 Regex options: Case insensitive Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Check whether each part of the domain is not longer than 63 characters:

`\b((?=[a-z0-9-]{1,63}\.)[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b`
 Regex options: Case insensitive Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Allow internationalized domain names using the punycode notation:

`\b((xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}\b`
 Regex options: Case insensitive Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Check whether each part of the domain is not longer than 63 characters, and allow internationalized domain names using the punycode notation:

`\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b`
 Regex options: Case insensitive Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

## Discussion

A domain name has the form of `domain.tld`, or `subdomain.domain.tld`, or any number of additional subdomains. ...

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required