Skip to main content

Text formula page

Extract Domain From URL Formula

Use this for campaign reports, link exports, or content calendars that need URLs grouped by domain.

Best for

Extract domain names from URLs in spreadsheets.

What it returns

If A2 is https://www.example.com/pricing, the result is example.com.

Copy formulas

Excel formula
=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))
Google Sheets formula
=REGEXEXTRACT(A2, "^(?:https?:\/\/)?(?:www\.)?([^\/\?#]+)")
Excel / Google Sheets difference

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 TextExample Result
Maya ChenMaya
SKU-1001-EastSKU
https://www.example.com/pricingexample.com
Acme North Acme North
What it returns

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 pieceRole in the formula
uThe trimmed URL source in the Excel LET formula.
pThe URL after removing the protocol when present.
hThe 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

HTTPS www host
=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

Query string host
=REGEXEXTRACT(A2, "^(?:https?:\/\/)?(?:www\.)?([^\/\?#]+)")

Google Sheets: Enter example.com?utm=1 in A2. Returns: example.com

Subdomain fragment host
=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

IssueLikely causeFix
Plain text produces an unexpected domainThe 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 expectedUserinfo, 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 functionThe 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

AlternativeWhen to use it
Extract Domain in Google SheetsUse for a worked Google Sheets example with URL rows.
REGEXEXTRACT Formula BuilderUse when the host pattern needs to be adjusted interactively.

Related formulas

Official references

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.