Formula example
Extract Domain from URL in Google Sheets Formula
Extract the domain from each URL in column A so tracking exports, backlink lists, or content reports can be grouped by site. For A2, the formula returns example.com.
Copyable formula
=REGEXEXTRACT(A2, "^(?:https?:\/\/)?(?:www\.)?([^\/\?#]+)")For https://www.example.com/pricing, the formula returns example.com.
Useful variations
=IFERROR(REGEXEXTRACT(A2, "^(?:https?:\/\/)?(?:www\.)?([^\/\?#]+)"), "")Keeps blank or invalid URL rows from showing a raw formula error.
=LOWER(IFERROR(REGEXEXTRACT(A2, "^(?:https?:\/\/)?(?:www\.)?([^\/\?#]+)"), ""))Use this before counting unique domains when source URLs mix uppercase and lowercase host names.
=REGEXEXTRACT(A2, "^(?:https?:\/\/)?([^\/\?#]+)")Use this when www.example.com and example.com should stay separate.
Sample data
| URL | Result |
|---|---|
| https://www.example.com/pricing | example.com |
| https://docs.google.com/spreadsheets | docs.google.com |
| https://support.example.com/examples | support.example.com |
When to use this formula
- You need the host name from a URL list before grouping, deduping, or counting domains.
- The source data is in Google Sheets and REGEXEXTRACT is available.
- You want to ignore http, https, and an optional www prefix.
How the regular expression works
The start anchor checks the beginning of the cell, the optional protocol part skips http or https, and the optional www part removes a common prefix. The captured group then keeps the domain text until a slash, question mark, or hash appears.
Use IFERROR for messy exports
URL exports often contain blank rows, notes, or malformed values. Wrap REGEXEXTRACT in IFERROR when the sheet should stay clean instead of showing #N/A for those rows.
Excel alternative
REGEXEXTRACT is a Google Sheets function. For Excel, use the related formula page with TEXTAFTER and TEXTBEFORE patterns, or use Power Query when URLs need more robust parsing.
Sample returned domains
| URL | Returned domain | Note |
|---|---|---|
| https://www.example.com/pricing | example.com | Protocol and www are removed. |
| https://docs.google.com/spreadsheets | docs.google.com | Subdomains are preserved. |
| https://support.example.com/examples | support.example.com | The path after the domain is ignored. |
Pattern parts
| Part | Meaning | Why it matters |
|---|---|---|
| ^ | Start of the cell | Prevents matching a later URL fragment first. |
| (?:https?:\/\/)? | Optional http or https protocol | Handles URLs with or without protocol text. |
| (?:www\.)? | Optional www prefix | Returns example.com instead of www.example.com. |
| ([^\/\?#]+) | Captured domain text | Stops before a path, query string, or hash. |
Formula explanation
- The pattern ignores optional http, https, and www prefixes.
- The captured group returns the text before the next slash, question mark, or hash.
- REGEXEXTRACT returns the first match from the source cell.
Common errors
- Cells without a matching URL return an error unless wrapped in IFERROR.
- Subdomains such as support.example.com are preserved; do not use this formula when you need only the root domain.
- URLs with ports, usernames, or unusual schemes may need a stricter parser.
- Excel users should not paste REGEXEXTRACT into older Excel versions.
Build your own version
Use the formula builder for this pattern: REGEXEXTRACT Formula Builder.
Related formulas
FAQ
Can I remove www from every URL?
Yes. The provided formula ignores one optional www prefix before returning the domain.
Can I use this in Excel?
REGEXEXTRACT is for Google Sheets. Use Excel text functions, the related Excel formula page, or Power Query for Excel workflows.
Does this return the root domain only?
No. It preserves subdomains such as docs.google.com. That is usually better for reporting, but root-domain extraction needs a different rule.
How do I avoid errors on blank URL cells?
Wrap the formula in IFERROR and return an empty string, such as =IFERROR(REGEXEXTRACT(A2, pattern), "").