Regex Guide · Prompt: “Match HTTP or HTTPS URLs and capture the domain in group 1 and path in group 2”

Extract Domain and Path From URLs Using Regex

Verified by LiveFormulaLab Engineering Team • Updated November 2026

Parse scheme, domain hostname, and path from HTTP/HTTPS URLs using a clean Regex pattern with capture groups. Test interactively live.

Verified Regex Syntax

https?:\/\/([a-zA-Z0-9.-]+)(\/[^\s]*)?

How This Formula Works (Step-by-Step)

Parsing URLs out of markdown docs, logs, or scraped HTML is straightforward with two capture groups.

Step 1: https?:\/\/ matches both http:// and https:// prefixes.

Step 2: ([a-zA-Z0-9.-]+) captures the domain name, and (\/[^\s]*)? optionally captures the path and query string.

Formula Syntax & Arguments Breakdown

Matches http:// or https:// protocols, isolates the domain hostname in Capture Group 1, and optionally captures the full trailing path, route segments, and query parameters in Capture Group 2.

Argument / ClauseRequirementData TypeRole & Behavior
https?:\/\/RequiredProtocol PrefixMatches either secure https:// or plain http:// schemes.
([a-zA-Z0-9.-]+)RequiredCapture Group 1 (Host)Captures subdomain and domain labels up to the first slash or space.
(\/[^\s]*)?OptionalCapture Group 2 (Path)Optionally captures the leading slash, route path, query parameters, and hash fragment.

3 Real-World Use Cases

  • Extracting all external links from support articles or Markdown files.
  • Grouping clickstream URLs by hostname inside Python or SQL pipelines.
  • Verifying allowed redirect domains in security middleware.

Cross-Platform Equivalents

Excel
=REGEXEXTRACT(A2, "https?://([a-zA-Z0-9.-]+)", 1)
SQL
SELECT REGEXP_SUBSTR(url, 'https?://([a-zA-Z0-9.-]+)') FROM clicks;
Python Pandas
df[['domain', 'path']] = df['url'].str.extract(r'https?://([a-zA-Z0-9.-]+)(\/[^\s]*)?')

Common Mistakes & Pro Tips

Pitfalls & How to Fix Them

  • Mistake: Capturing [\S]* on Markdown links like [Docs](https://liveformulalab.com/library) swallows the trailing ")" parenthesis.

    Fix: Exclude punctuation or closing brackets at the boundary: (\/[^\s)"'>]*)?.

  • Mistake: Leaving out the "?" after the path group causes bare homepage URLs like https://liveformulalab.com (no trailing slash) to fail.

    Fix: Keep the path capture group optional with (...)? so root domains match cleanly.

  • Mistake: Including port numbers like :3000 inside hostname extraction breaks domain-level grouping if ":" is missing or mishandled.

    Fix: Explicitly allow or exclude ports (?:\:\d+)? depending on whether you want host:port or pure hostname.

Engineering Pro Tips

  • Prefix Group 1 with (?:www\.)? when grouping analytics traffic so www.domain.com and domain.com merge cleanly.
  • In JavaScript, pair regex extraction with the built-in new URL(str) parser when validating untrusted user redirect URLs.
  • Use the global flag (g) to extract every outbound URL inside a multi-paragraph blog post or support email.
  • Strip UTM tracking parameters separately after extracting Group 2 by splitting at the "?" query delimiter.

Frequently Asked Questions

How do I strip "www." from the captured domain group?
Add a non-capturing (?:www\.)? right before ([a-zA-Z0-9.-]+).
What if a URL appears inside parentheses in Markdown?
Exclude closing parentheses in the path class: (\/[^\s)]*)?.
Does this match URLs with query parameters like ?ref=home?
Yes, [^\s]* matches path segments, query strings, and fragments until whitespace.

Related Formulas & Guides