2014-10-02 84 views
0

Ive得到的URL列表这样剥离HTTP://和使用Excel

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

正如你可以看到/网站的一个孤单混合物无需http://,有/无子页面,子页面与/路径/没有www.

即时消息试图做的是消毒清单,使其读取像这样(下面)即。删除http://,删除子页面,但保留www.(如果存在)。

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

在Google Spreadsheets中做到这一点的最佳方法是什么? Idealy像php的parse_url()函数会很棒。我一直在使用=MID(A1, FIND("//", A1)+2, FIND("/",A1,10)-8)进行试验,但问题在于它剥离了www.,有没有更好的方法来做到这一点,最好使用自定义函数而不是宏,以便它可以在Excel和Google Spreadsheets中使用。

回答

1

您可以使用公式做到这一点。同样的公式适用于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 
) 
) 
1

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 
1

这个公式将做到这一点:

=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 =

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


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

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

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

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


3。 anothersite.com/somepage

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

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

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 =

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


5. www.yetanothersite.com

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

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

LEFT("www.yetanothersite.com",22) =
www.yetanothersite.com