^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.[A-Za-z0-9-]{1,63})*\.[A-Za-z]{2,}$ This pattern validates a hostname made of dot-separated labels: each label is 1-63 alphanumeric or hyphen characters that neither starts nor ends with a hyphen, and the final label is an alphabetic TLD of at least two letters. Validation patterns answer a yes/no question about an entire input: does this string conform to the expected format? They are anchored with ^ and $ so the whole value must match, and they belong at the edge of your system — form fields, API payloads and import files — where rejecting bad input early prevents downstream errors. A validation regex is a contract check, not a transformation. It needs no special flags. Apply it to the whole input as a format contract. Lookbehind (?<!-) is unsupported in some older engines (e.g. legacy Safari); provide a fallback if you target them. It does not verify that the TLD actually exists or that the domain resolves — combine with DNS or a public-suffix list for real validation. Internationalized domain names must be punycode-encoded (xn--) before this ASCII pattern will accept them. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.
Token-by-token explanation
^ — start anchor(?!-) — negative lookahead: label must not start with a hyphen[A-Za-z0-9-]{1,63} — label body, 1 to 63 characters(?<!-) — negative lookbehind: label must not end with a hyphen(\.[A-Za-z0-9-]{1,63})* — zero or more additional dot-prefixed labels\.[A-Za-z]{2,}$ — a final alphabetic TLD of two or more letters
What it matches
example.comsub.domain.co.ukmy-site.org What it does not match
-bad.combad-.comexampleexample.cexa mple.com When to use it
Reach for a validation pattern when data crosses a trust boundary: a signup form, an API request body, a CSV import or a configuration value. Run it before any processing so malformed input is rejected with a clear message instead of causing a cryptic failure later. Keep the pattern in one shared place, document the exact format it accepts, and pair it with a real parser when the value has semantics (dates, numbers, addresses) that a shape check alone cannot confirm.
Caveats
- Lookbehind (?<!-) is unsupported in some older engines (e.g. legacy Safari); provide a fallback if you target them.
- It does not verify that the TLD actually exists or that the domain resolves — combine with DNS or a public-suffix list for real validation.
- Internationalized domain names must be punycode-encoded (xn--) before this ASCII pattern will accept them.
Code examples
const re = /^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.[A-Za-z0-9-]{1,63})*\.[A-Za-z]{2,}$/;
const input = "example.com";
if (re.test(input)) {
// valid: proceed
} else {
// reject with a helpful message
} import re
rx = re.compile(r"^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.[A-Za-z0-9-]{1,63})*\.[A-Za-z]{2,}$")
value = "example.com"
if rx.fullmatch(value):
pass # valid
else:
raise ValueError("invalid format") import java.util.regex.*;
Pattern p = Pattern.compile("^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\\.[A-Za-z0-9-]{1,63})*\\.[A-Za-z]{2,}$");
Matcher m = p.matcher("example.com");
boolean ok = m.matches(); // whole input must conform <?php
$subject = "example.com";
if (preg_match('/^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.[A-Za-z0-9-]{1,63})*\.[A-Za-z]{2,}$/', $subject, $m)) {
// $m[0] is the full match
} How to test it
Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.
Frequently asked questions
Why limit labels to 63 characters?
DNS specifies a maximum of 63 octets per label and 253 for the full name, so the {1,63} bound mirrors the real protocol constraint.
Does this accept localhost?
No — it requires a dotted alphabetic TLD. Single-label hosts like localhost or intranet names need a separate, looser pattern.
Should validation regexes be anchored?
Yes — use ^ and $ (or fullmatch) so the entire input must conform. Without anchors, a valid-looking substring inside garbage would pass, letting malformed data through your validation layer.
What is catastrophic backtracking and how do I avoid it?
Nested quantifiers like (a+)+ can explode exponentially on non-matching input, freezing the engine (ReDoS). Avoid ambiguous nesting, prefer atomic groups or possessive quantifiers where supported, and bound input length.
Try the Regex Tester
Live regex matching with groups, flags and explanations.