剥离http://和使用excel的子页面/path

我有这样的url列表

http://example.com http://www.somesite.com/mypage anothersite.com/somepage http://example.com/anotherpage www.yetanothersite.com 

正如你可以看到有/没有http:// ,有/没有子页面,有/没有www.的站点混合www.

我试图做的是清理清单,所以它读取像这样(下)即。 删除http:// ,删除子页面,但离开www. 如果有的话。

 example.com www.somesite.com anothersite.com example.com www.yetanothersite.com 

Google电子表格中最好的方法是什么? 理想的东西像php的parse_url()函数将是很好的。 我一直在尝试使用=MID(A1, FIND("//", A1)+2, FIND("/",A1,10)-8)但这个问题是剥离www. ,有没有更好的方法来做到这一点,最好使用自定义函数,而不是macros,以便它可以在Excel和谷歌电子表格。

你可以使用公式来做到这一点。 Google表格和Excel都可以使用相同的公式。

 =MID(A2,IFERROR(SEARCH("//",A2)+2,1),IFERROR(SEARCH("/",A2,IFERROR(SEARCH("//",A2)+2,1))-IFERROR(SEARCH("//",A2)+2,1),LEN(A2))) 

如果你把它分解的话,这有点长,难看,但不难理解

 =MID(A2, 'Take a slice from the middle of the string in cell A2 IFERROR( 'Return the value found in the next statement, unless its an error SEARCH("//",A2)+2 'Search the string for // and take the position after it ,1 'otherwise start from the start of the string if its not found ) , IFERROR( SEARCH("/",A2, 'Search for / in the string IFERROR(SEARCH("//",A2)+2,1) 'Start after the // if it was found ) -IFERROR( SEARCH("//",A2)+2,1) 'Since the second parameter of mid is a length 'not a position, subtract the location of // 'again if it was found , LEN(A2) 'Otherwise take all of the remaining string ) ) 

VBA; 所有之前切断/然后阅读,直到下一个/

 Function getDomain(url As String) As String Dim pos As Long pos = InStr(url, "//") If (pos > 0) Then url = Mid$(url, 2 + pos) End If pos = InStr(url, "/") If (pos > 0) Then url = Left$(url, pos - 1) End If getDomain = url End Function 

这个公式将做到这一点:

 =LEFT(SUBSTITUTE(A1,"http://",""),FIND("/",SUBSTITUTE(A1&"/","http://",""))-1) 

使用你的例子:

  1. http://example.com

SUBSTITUTE(A1,"http://","") =
example.com

FIND("/",SUBSTITUTE(A1&"/","http://",""))-1 =
FIND("/","example.com/")-1 =
11

LEFT("example.com",11) =
example.com


2. http://www.somesite.com/mypage

SUBSTITUTE(A1,"http://","") =
http://www.somesite.com/mypage

FIND("/",SUBSTITUTE(A1&"/","http://",""))-1 =
FIND("/","www.somesite.com/mypage/")-1 =
16

LEFT("www.somesite.com/mypage",16)
http://www.somesite.com


3. anothersite.com/somepage

SUBSTITUTE(A1,"http://","") =
anothersite.com/somepage

FIND("/",SUBSTITUTE(A1&"/","http://",""))-1 =
FIND("/","anothersite.com/somepage/")-1 =
15

LEFT("anothersite.com/somepage",15)
anothersite.com


4. http://example.com/anotherpage

SUBSTITUTE(A1,"http://","") =
example.com/anotherpage

FIND("/",SUBSTITUTE(A1&"/","http://",""))-1 =
FIND("/","example.com/anotherpage/")-1 =
11

LEFT("example.com/anotherpage",11)
example.com


5. www.yetanothersite.com

SUBSTITUTE(A1,"http://","") =
http://www.yetanothersite.com

FIND("/",SUBSTITUTE(A1&"/","http://",""))-1 =
FIND("/","www.yetanothersite.com/")-1 =
22

LEFT("www.yetanothersite.com",22) =
http://www.yetanothersite.com