Subdomain Extraction Regex for Python
/^(?:([a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+)?[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z]{2,63})$/iWhat this pattern does
This page provides a comprehensive, battle-tested regular expression for matching subdomain extraction, ported and verified for Python. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.
Python Implementation
# Subdomain Extraction
# ReDoS-safe | RegexVault — Web & Network > Domain
import re
subdomain_extraction_pattern = re.compile(r'^(?:([a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+)?[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z]{2,63})$')
def validate_subdomain_extraction(value: str) -> bool:
return bool(subdomain_extraction_pattern.fullmatch(value))
# Example
print(validate_subdomain_extraction("www.example.com")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
www.example.com | -invalid.example.com |
api.v2.example.com | example.c |
deep.sub.domain.example.com | .example.com |
example.com | example |
a.b.c.d.example.io | — |
When to use this pattern
This pattern is drawn from the Web & Network > Domain category and carries a ReDoS-safe certification. That matters for Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Without PSL, co.uk domains would incorrectly identify uk as the TLD and co as the SLD. Use the publicsuffix library for correct eTLD+1 parsing.
Technical Notes
Capture group 1 contains the subdomain prefix (with trailing dot) if present, empty/null if it's a bare SLD+TLD. PSL (Public Suffix List) is needed for accurate eTLD+1 extraction.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern