Text formula page
Extract Domain From URL Formula
Use this for campaign reports, link exports, or content calendars that need URLs grouped by domain.
Extract domain names from URLs in spreadsheets.
If A2 is https://www.example.com/pricing, the result is example.com.
Copy formulas
=LET(u,TRIM(A2),p,IFERROR(TEXTAFTER(u,"//"),u),h,TEXTBEFORE(SUBSTITUTE(SUBSTITUTE(p,"?","/"),"#","/")&"/","/"),IF(LEFT(h,4)="www.",RIGHT(h,LEN(h)-4),h))=REGEXEXTRACT(A2, "^(?:https?:\/\/)?(?:www\.)?([^\/\?#]+)")Both formulas accept a URL with or without a protocol and stop before a path, query, or fragment. Excel uses modern text functions; Google Sheets uses REGEXEXTRACT.
Example data
| Raw Text | Example Result |
|---|---|
| Maya Chen | Maya |
| SKU-1001-East | SKU |
| https://www.example.com/pricing | example.com |
| Acme North | Acme North |
If A2 is https://www.example.com/pricing, the result is example.com.
How the formula works
- The formula removes www when present.
- It extracts the host before the next slash, question mark, or hash.
- Use IFERROR if some cells are not URLs.
| Syntax piece | Role in the formula |
|---|---|
| u | The trimmed URL source in the Excel LET formula. |
| p | The URL after removing the protocol when present. |
| h | The host portion before slash, query, or fragment boundaries. |
| www. | The optional prefix removed from the final Excel host output and matched by the Sheets regex. |
Verified examples
=LET(u,TRIM(A2),p,IFERROR(TEXTAFTER(u,"//"),u),h,TEXTBEFORE(SUBSTITUTE(SUBSTITUTE(p,"?","/"),"#","/")&"/","/"),IF(LEFT(h,4)="www.",RIGHT(h,LEN(h)-4),h))Excel: Enter https://www.example.com/pricing in A2. Returns: example.com
=REGEXEXTRACT(A2, "^(?:https?:\/\/)?(?:www\.)?([^\/\?#]+)")Google Sheets: Enter example.com?utm=1 in A2. Returns: example.com
=LET(u,TRIM(A2),p,IFERROR(TEXTAFTER(u,"//"),u),h,TEXTBEFORE(SUBSTITUTE(SUBSTITUTE(p,"?","/"),"#","/")&"/","/"),IF(LEFT(h,4)="www.",RIGHT(h,LEN(h)-4),h))Excel: Enter https://sub.example.com#pricing in A2. Returns: sub.example.com
Common errors and fixes
| Issue | Likely cause | Fix |
|---|---|---|
| Plain text produces an unexpected domain | The formula assumes the cell contains a host or URL-like value. | Validate the input format and add an explicit URL check before extracting hosts. |
| A complex URL is not parsed as expected | Userinfo, ports, internationalized hosts, or unusual URL syntax needs more parsing rules than this pattern provides. | Use a URL-aware parser outside the spreadsheet or constrain the accepted input format. |
| Excel reports an unknown function | The workbook does not support modern LET, TEXTAFTER, or TEXTBEFORE. | Use a supported Excel version or the Google Sheets regex version. |
When not to use this formula
- Do not use this formula as a complete URL validator or for security-sensitive URL parsing.
Alternatives
| Alternative | When to use it |
|---|---|
| Extract Domain in Google Sheets | Use for a worked Google Sheets example with URL rows. |
| REGEXEXTRACT Formula Builder | Use when the host pattern needs to be adjusted interactively. |
Related formulas
Official references
- TEXTBEFORE function from Microsoft
- TEXTAFTER function from Microsoft
- REGEXEXTRACT function from Google
FAQ
Is the protocol required?
No. Both formulas accept values with http or https and host-like values without a protocol.
Are query strings and fragments included?
No. The formulas stop before ? and #, so tracking parameters and fragment labels are excluded.