Is there a regex to validate zone names in DNS ?
- User
Is there a regex to validate zone names in DNS ?
- Assistant
Below is a practical “good-enough” regular expression for most workloads, followed by a longer discussion in case you want to tune it, support IDNs, allow underscores (for SRV records), wild-cards, etc.
──────────────────────────────────────── A pragmatic DNS zone-name validator ────────────────────────────────────────
^(?=.{1,253}\.?$)(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}\.?$Quick facts • Whole name ≤ 253 chars (plus an optional trailing “.”).
• Each label 1-63 chars.
• Labels start & end with a letter/number; interior may have “-”.
• Final label (TLD/zone apex) must be alphabetic, 2-63 chars.
• Trailing dot allowed (canonical form in zone files) but not required.
• Case-insensitive: use the i-flag or[A-Za-z].Example (Python):
import re rx = re.compile(r'^(?=.{1,253}\.?$)(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}\.?$', re.I) for name in ['example.com', 'sub.example.com.', 'a-b.c-d.net', 'bad..dots', '-dash.example', 'toolong' * 40 + '.com']: print(name, '=>', bool(rx.fullmatch(name)))──────────────────────────────────────── What the pieces do ────────────────────────────────────────
(?=.{1,253}\.?$)– forward-lookahead that limits the whole string to 1–253 characters, ignoring a possible final dot.(?: ... \.)+– one or more labels followed by a “.”.
• First char[A-Za-z0-9].
• Middle(?:[A-Za-z0-9-]{0,61}…) up to 63 total.
• Last char[A-Za-z0-9](so no trailing “-”).[A-Za-z]{2,63}– the last label must be 2–63 letters (typical TLD rule).\.?$– optional dot at the very end.
──────────────────────────────────────── Do you need more (or less)? ────────────────────────────────────────
-
Single-label zones (e.g. “localhost.”): Remove the final-label restriction:
(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.?)+ -
Root (@) / null label: Treat “.” as special outside the regex – easier than encoding it.
-
Wild-cards (“*.example.”): Prefix the pattern with
(?:\*\.)?. -
SRV & TXT names that allow “_”: Replace
[A-Za-z0-9]with[A-Za-z0-9_]for the first/last char. -
Internationalised domain names (IDN): • After IDNA-to-Punycode conversion, the same regex works.
• Or extend[A-Za-z]with\p{L}in a Unicode-aware engine. -
Don’t care about TLD rules: End with the general label pattern instead of
[A-Za-z]{2,63}.
──────────────────────────────────────── Reference RFC snippets ──────────────────────────────────────── • RFC 1035 §2.3.1 – label 1-63, name ≤ 255 octets, letters/digits/“-”.
• RFC 1123 §2.1 – labels may start with digits.
• RFC 5890/5891 – IDN & Punycode (xn--…).──────────────────────────────────────── TL;DR If you just need to block obvious garbage, the first regex is compact and portable. Tighten or relax it according to the bullet points above, but remember that the DNS protocol ultimately decides what is legal—regular expressions can only approximate that truth. 😉
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.