Extract DomainName from URL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Ayzin

    Extract DomainName from URL

    Hi,

    I have a var-sized URL passed into my method. I need to trim it, so instead
    of :

    "123abc.MyDomai n.com", I ended up with "MyDomain". The size of the initial
    string is not fixed. IndexOf wouldn't work, because different domain names
    are passed every time. I think, I need to use RegEx, but maybe there is some
    trick that I just didn't think of. Please, advise.
    Thanks in advance,
    --Alex Ayzin


  • One Handed Man [ OHM# ]

    #2
    Re: Extract DomainName from URL

    This needs to be converted to VB.NET but here is a function which will
    return any part of the URL depending on the strPart.

    At least the logic is there for you to deal use.

    Regards - OHM#


    Function ParseURL(strURL , strPart)

    'descr: parses a portion of a url
    'strURL: the URL to parse
    'strPart: the part to get. Allowed values are:
    ' protocol, server, domain, path, file, hash, query

    Dim arrTemp
    Dim strTemp
    Dim nPos

    On Error Resume Next

    Select Case strPart
    Case "protocol"
    'return the protocol, eg. http://, ftp://
    nPos = InStr(strURL, ":") + 1
    Do Until (Mid(strURL, nPos, 1) <> "/") And (Mid(strURL, nPos, 1) <>
    "\")
    nPos = nPos + 1
    Loop
    ParseURL = Left(strURL, nPos - 1)
    Case "server"
    'return the server, eg. www.microsoft.com
    strTemp = ParseURL(strURL , "protocol")
    strURL = Right(strURL, Len(strURL) - Len(strTemp))
    If InStr(strURL, "/") Then
    strTemp = Left(strURL, InStr(strURL, "/") - 1)
    ElseIf InStr(strURL, "\") Then
    strTemp = Left(strURL, InStr(strURL, "\") - 1)
    End If
    If InStr(strTemp, "@") Then
    'remove user/password combo, return only the server
    ParseURL = Right(strTemp, Len(strTemp) - InStr(strTemp, "@"))
    Else
    ParseURL = strTemp
    End If
    Case "domain"
    'return only the domain, eg. amazon.com, wa.gov, etc
    strTemp = ParseURL(strURL , "server")
    arrTemp = Split(strTemp, ".")
    ParseURL = arrTemp(UBound( arrTemp) - 1) & "." &
    arrTemp(UBound( arrTemp))
    Case "path"
    'return the path
    If InStr(strURL, "#") Then strURL = Left(strURL, InStr(strURL,
    "#") - 1)
    If InStr(strURL, "?") Then strURL = Left(strURL, InStr(strURL,
    "?") - 1)
    If InStrRev(strURL , "/") > InStrRev(strURL , "\") Then
    ParseURL = Left(strURL, InStrRev(strURL , "/"))
    ElseIf InStrRev(strURL , "\") > InStrRev(strURL , "/") Then
    ParseURL = Left(strURL, InStrRev(strURL , "\"))
    End If
    Case "file"
    'return the filename only
    If InStr(strURL, "#") Then strURL = Left(strURL, InStr(strURL,
    "#") - 1)
    If InStr(strURL, "?") Then strURL = Left(strURL, InStr(strURL,
    "?") - 1)
    If InStrRev(strURL , "/") > InStrRev(strURL , "\") Then
    ParseURL = Right(strURL, Len(strURL) - InStrRev(strURL , "/"))
    ElseIf InStrRev(strURL , "\") > InStrRev(strURL , "/") Then
    ParseURL = Right(strURL, Len(strURL) - InStrRev(strURL , "\"))
    End If
    Case "hash"
    'return the bookmark (hash) without the hash mark
    If InStr(strURL, "#") Then
    arrTemp = Split(strURL, "#")
    strTemp = arrTemp(UBound( arrTemp))
    If InStr(strTemp, "?") Then
    ParseURL = Left(strTemp, InStr(strTemp, "?") - 1)
    Else
    ParseURL = strTemp
    End If
    Else
    ParseURL = ""
    End If
    Case "query"
    'return the query string without the question mark
    If InStr(strURL, "?") Then
    arrTemp = Split(strURL, "?")
    strTemp = arrTemp(UBound( arrTemp))
    If InStr(strTemp, "#") Then
    ParseURL = Left(strTemp, InStr(strTemp, "#") - 1)
    Else
    ParseURL = strTemp
    End If
    Else
    ParseURL = ""
    End If
    End Select

    If Err.Number <> 0 Then ParseURL = ""

    End Function


    Alex Ayzin wrote:[color=blue]
    > Hi,
    >
    > I have a var-sized URL passed into my method. I need to trim it, so
    > instead of :
    >
    > "123abc.MyDomai n.com", I ended up with "MyDomain". The size of the
    > initial string is not fixed. IndexOf wouldn't work, because different
    > domain names are passed every time. I think, I need to use RegEx, but
    > maybe there is some trick that I just didn't think of. Please, advise.
    > Thanks in advance,
    > --Alex Ayzin[/color]

    Regards - OHM# OneHandedMan@BT Internet.com


    Comment

    • Cor

      #3
      Re: Extract DomainName from URL

      Hi Alex,

      I thought there is a better method, but with indexof it works of course also
      Rough written
      \\\
      Dim Alex as string = "123abc.MyDomai n.com"
      Alex = Alex.substring( alex.indexof(". "))
      ///

      I did not do some action to delete the com because that is a part of the
      domain name.

      I hope this works for you.

      Cor
      [color=blue]
      > "123abc.MyDomai n.com", I ended up with "MyDomain". The size of the initial
      > string is not fixed. IndexOf wouldn't work, because different domain names
      > are passed every time. I think, I need to use RegEx, but maybe there is[/color]
      some[color=blue]
      > trick that I just didn't think of. Please, advise.
      > Thanks in advance,
      > --Alex Ayzin
      >
      >[/color]


      Comment

      • Ken Tucker [MVP]

        #4
        Re: Extract DomainName from URL

        Hi,

        Dim strURL As String = "123abc.MyDomai n.com"

        Dim arParts() As String

        arParts = strURL.Split(". ")

        Debug.WriteLine (arParts(1))

        Ken

        --------------------------

        "Alex Ayzin" <Alex.Ayzin@ver izon.net> wrote in message
        news:ONaczW5vDH A.540@tk2msftng p13.phx.gbl...[color=blue]
        > Hi,
        >
        > I have a var-sized URL passed into my method. I need to trim it, so[/color]
        instead[color=blue]
        > of :
        >
        > "123abc.MyDomai n.com", I ended up with "MyDomain". The size of the initial
        > string is not fixed. IndexOf wouldn't work, because different domain names
        > are passed every time. I think, I need to use RegEx, but maybe there is[/color]
        some[color=blue]
        > trick that I just didn't think of. Please, advise.
        > Thanks in advance,
        > --Alex Ayzin
        >
        >[/color]


        Comment

        • Alex Ayzin

          #5
          Re: Extract DomainName from URL

          If it's not too late, thanks a lot, guys. It really helped me(I blocked out
          there, probably). I actually used Ken's solution. Thanks again to all of
          you.
          --Alex Ayzin

          "Ken Tucker [MVP]" <vb2ae@bellsout h.net> wrote in message
          news:OtJpkn9vDH A.1996@TK2MSFTN GP12.phx.gbl...[color=blue]
          > Hi,
          >
          > Dim strURL As String = "123abc.MyDomai n.com"
          >
          > Dim arParts() As String
          >
          > arParts = strURL.Split(". ")
          >
          > Debug.WriteLine (arParts(1))
          >
          > Ken
          >
          > --------------------------
          >
          > "Alex Ayzin" <Alex.Ayzin@ver izon.net> wrote in message
          > news:ONaczW5vDH A.540@tk2msftng p13.phx.gbl...[color=green]
          > > Hi,
          > >
          > > I have a var-sized URL passed into my method. I need to trim it, so[/color]
          > instead[color=green]
          > > of :
          > >
          > > "123abc.MyDomai n.com", I ended up with "MyDomain". The size of the[/color][/color]
          initial[color=blue][color=green]
          > > string is not fixed. IndexOf wouldn't work, because different domain[/color][/color]
          names[color=blue][color=green]
          > > are passed every time. I think, I need to use RegEx, but maybe there is[/color]
          > some[color=green]
          > > trick that I just didn't think of. Please, advise.
          > > Thanks in advance,
          > > --Alex Ayzin
          > >
          > >[/color]
          >
          >[/color]


          Comment

          Working...