473,320 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Extract DomainName from URL

Hi,

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

"123abc.MyDomain.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
Nov 20 '05 #1
4 2956
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:
Hi,

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

"123abc.MyDomain.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


Regards - OHM# On**********@BTInternet.com
Nov 20 '05 #2
Cor
Hi Alex,

I thought there is a better method, but with indexof it works of course also
Rough written
\\\
Dim Alex as string = "123abc.MyDomain.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
"123abc.MyDomain.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

Nov 20 '05 #3
Hi,

Dim strURL As String = "123abc.MyDomain.com"

Dim arParts() As String

arParts = strURL.Split(".")

Debug.WriteLine(arParts(1))

Ken

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

"Alex Ayzin" <Al********@verizon.net> wrote in message
news:ON*************@tk2msftngp13.phx.gbl...
Hi,

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

"123abc.MyDomain.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

Nov 20 '05 #4
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]" <vb***@bellsouth.net> wrote in message
news:Ot**************@TK2MSFTNGP12.phx.gbl...
Hi,

Dim strURL As String = "123abc.MyDomain.com"

Dim arParts() As String

arParts = strURL.Split(".")

Debug.WriteLine(arParts(1))

Ken

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

"Alex Ayzin" <Al********@verizon.net> wrote in message
news:ON*************@tk2msftngp13.phx.gbl...
Hi,

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

instead
of :

"123abc.MyDomain.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


Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Don Crossman | last post by:
Assume a MYSQL table, foo. One column, bar datetime. Two rows: 2004-01-01 08:00:00 2004-02-01 08:00:00 select * from foo where extract(day from bar)=1; 2 rows in set...
3
by: Phong Ho | last post by:
Hi everyone, I try to write a simple web crawler. It has to do the following: 1) Open an URL and retrieve a HTML file. 2) Extract news headlines from the HTML file 3) Put the headlines into a...
9
by: Sharon | last post by:
hi, I want to extract a string from a file, if the file is like this: 1 This is the string 2 3 4 how could I extract the string, starting from the 10th position (i.e. "T") and...
10
by: Robert Schultz | last post by:
I have a C/C++ file that I simply want to 'extract' a function from. Something like: extract <function name> <c or cpp file> I want it to return from the beginning of the function, to the end. ...
6
by: Mohammad-Reza | last post by:
Hi I want to extract icon of an exe file and want to know how. I look at the MSDN and find out that I can use ExtractIconEx() Windows API but in there are some changes to that api in c# I made...
3
by: jarod1701 | last post by:
Hi, I'm currently trying to create a regular expression that can extract certain elements from a url. The url will be of the following form: http://user:pass@www.sitename.com I want a...
8
by: Fabian Braennstroem | last post by:
Hi, I would like to remove certain lines from a log files. I had some sed/awk scripts for this, but now, I want to use python with its re module for this task. Actually, I have two different...
2
by: shubhangi | last post by:
Hi, I'm trying to run a job. The script contains a code which access remote server. The job fails. The same job when pointed to local server runs successfully. Error given when execting a job...
1
by: rcamarda | last post by:
I'd need to have a function that allows me to extract 'fields' from within the string I.E. (kinda pseudo code) declare @foo as varchar(100) set @foo = "Robert*Camarda*123 Main Street" select...
5
by: Steve | last post by:
Hi all Does anybody please know a way to extract an Image from a pdf file and save it as a TIFF? I have used a scanner to scan documents which are then placed on a server, but I need to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.