Web
Subdomain Hostname Regex
Many applications give each customer or service its own subdomain, such as acme.example.com or api.example.com. This pattern matches lowercase hostnames that have at least three labels, so a bare domain like example.com is rejected, and it captures the left-most label in group 1. That makes it handy for multi-tenant routing, cookie scoping checks and DNS record forms. Labels cannot start or end with a dash. The honest limit is that it cannot use the public suffix list, so a host such as example.co.uk is treated as having a subdomain.
Pattern (JavaScript)
/^([a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\.(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/Starts at the start of the line/string, then a group (one of the characters a-z0-9, then (one of the characters a-z0-9-, zero or more times, then one of the characters a-z0-9), optionally (zero or one time)), then the character ".", then (one of the characters a-z0-9, then (one of the characters a-z0-9-, zero or more times, then one of the characters a-z0-9), optionally (zero or one time), then the character "."), one or more times, then a lowercase letter, 2 or more times, then the end of the line/string.
Examples
Matches
- ✓ api.example.com
- ✓ blog.example.org
- ✓ a.b.example.test
Does not match
- ✗ example.com
- ✗ .example.com
- ✗ API.example.com
Try it
0 matches (JavaScript engine)
api.example.com
blog.example.org
a.b.example.test
example.com
.example.com
API.example.comCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^([a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\.(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/g;
const text = "api.example.com";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^([a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\.(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$')
text = 'api.example.com'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^([a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\.(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/';
$text = 'api.example.com';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^([a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\\.(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z]{2,}$");
String text = "api.example.com";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^([a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\.(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$`)
text := "api.example.com"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^([a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\.(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$");
var text = "api.example.com";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^([a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\.(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$").unwrap();
let text = "api.example.com";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^([a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\.(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/
text = 'api.example.com'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why is example.co.uk treated as a subdomain?
- Some countries use two-part public suffixes like co.uk. A regex cannot know those without a list, so use a public suffix library when you need the registrable domain.
- How do I extract the tenant name from the host?
- Read capture group 1 after a match. For api.example.com it returns api, which you can then look up in your tenant table.
- Why is uppercase rejected?
- Hostnames are case-insensitive, and browsers send them in lowercase. Lowercase the host before testing, or add the i flag to accept any case.
Related patterns
- Domain Name^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}$
- Data URI^data:([a-z]+/[a-z0-9.+-]+)?(?:;[a-z0-9-]+=[a-z0-9-]+)*(;base64)?,[A-Za-z0-9+/=%._~-]*$
- File Path with Extension^(?:[A-Za-z]:[\\/]|/)?(?:[\w .-]+[\\/])*[\w .-]+\.[A-Za-z0-9]{1,8}$
- HTML Tag</?([A-Za-z][A-Za-z0-9-]*)(?:\s[^<>]*)?/?>