Everybody here seems to focus on the .com
or .net
, but domains don't always end with .com
or .net
... however, they always end before the /
.
So first find where the host name begins:
=FIND("://", A1) + LEN("://") =FIND("://", A1) + 3
Then find the first slash afterwards – that's where the host name ends:
=FIND("/", A1, FIND("://", A1) + 3)
Or, just in case there's no path at all:
=IFERROR(FIND("/", A1, FIND("://", A1) + 3), LEN(A1) + 1)
And extract everything in between:
=MID(A1;FIND("://";A1)+3;IFERROR(FIND("/";A1;FIND("://";A1)+3);LEN(A1)+1)-(FIND("://";A1)+3)) A1: https://www.example.com/foo/bar A2: =FIND("://";A1)+3 A3: =IFERROR(FIND("/";A1;A2);LEN(A1)+1) A4: =MID(A1;A2;A3-A2)
If you want to throw away the leftmost domain component, you can use FIND(".",A1)
:
=MID(A1;FIND(".";A1)+1;IFERROR(FIND("/";A1;FIND(".";A1)+1);LEN(A1)+1)-(FIND(".";A1)+1)) A1: https://www.example.com/foo/bar A2: =FIND(".";A1)+1 A3: =IFERROR(FIND("/";A1;A2);LEN(A1)+1) A4: =MID(A1;A2;A3-A2)
For Salesforce, with no error checks:
=MID(A1,FIND("://",A1)+3,FIND("/",A1,FIND("://",A1)+3)-(FIND("://",A1)+3)) =MID(A1,FIND(".",A1)+1,FIND("/",A1,FIND(".",A1)+1)-(FIND(".",A1)+1))
Note: This still has a few assumptions – e.g. it won't work right if the URLs have a username@domain
or username:password@domain
. The FIND(".")
version also assumes there are at least two domain components and won't work right with https://example/foo
– it needs some extra IFERROR() checks for that.