Back to Home

Domain Name Validation Regex

CronOS Team
regexdomainvalidationtutorialdns

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern

Domain Name Validation Regex

Validate domain names with proper subdomain and TLD structure using regex pattern for lowercase alphanumeric domains.

Pattern Breakdown

regex
^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$

Components

ComponentDescriptionMatches
^Start anchorEnsures match from string start
([a-z0-9]+(-[a-z0-9]+)*\.)+Domain partsOne or more domain segments
[a-z]{2,}TLDTwo or more lowercase letters
$End anchorEnsures match to string end

Detailed Breakdown

  • ([a-z0-9]+(-[a-z0-9]+)*\.)+ - Domain parts (one or more):
    • [a-z0-9]+ - One or more lowercase letters or digits
    • (-[a-z0-9]+)* - Zero or more hyphen-separated segments
    • \. - Literal dot separator
    • + - One or more domain parts (allows subdomains)
  • [a-z]{2,} - TLD: two or more lowercase letters

Examples

Valid:

  • example.com
  • subdomain.example.com
  • multi-level.example.com
  • test-domain.co.uk
  • example123.org
  • my-site.io

Invalid:

  • example (missing TLD)
  • example.c (TLD too short)
  • EXAMPLE.COM (uppercase not allowed)
  • example..com (double dot)
  • -example.com (starts with hyphen)
  • example-.com (ends with hyphen)
  • example.com. (trailing dot)

Implementation

JavaScript

javascript
const domainRegex = /^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/;
domainRegex.test('example.com'); // true
domainRegex.test('subdomain.example.com'); // true
domainRegex.test('test-domain.co.uk'); // true
domainRegex.test('example'); // false (missing TLD)
domainRegex.test('EXAMPLE.COM'); // false (uppercase)

Python

python
import re
domain_regex = r'^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$'
bool(re.match(domain_regex, 'example.com'))  # True
bool(re.match(domain_regex, 'subdomain.example.com'))  # True
bool(re.match(domain_regex, 'example'))  # False (missing TLD)

Go

go
domainRegex := regexp.MustCompile(`^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$`)
domainRegex.MatchString("example.com") // true
domainRegex.MatchString("subdomain.example.com") // true
domainRegex.MatchString("example") // false (missing TLD)

Limitations

  1. Lowercase only: Only accepts lowercase letters
  2. No underscore: Doesn't allow underscores in domain names
  3. No multi-part TLD validation: Doesn't specifically validate .co.uk format
  4. No length limits: Doesn't enforce DNS length limits (253 chars total, 63 per label)
  5. No IDN support: Doesn't support internationalized domain names
  6. Format only: Validates format, not actual DNS existence

When to Use

  • Domain name format validation
  • DNS-related applications
  • Subdomain validation
  • When you need strict format checking
  • Domain input forms

For production, consider:

  • Supporting uppercase (case-insensitive)
  • Adding length validation (max 253 chars, 63 per label)
  • Validating against known TLD lists
  • Supporting internationalized domain names
  • Combining with DNS lookup for existence verification

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern