[thelist] Parsing URLs in ASP.net

Norman Beresford n.beresford at anansi.co.uk
Tue Jun 4 10:36:04 CDT 2002


Hi Jason

You're going to be using regular expressions to deal with this.  Bascially
you want to force your users to input URL's in a set format.  I've got the
backend to a CMS which converts URLs into XML tags, the only cravet being
that the user must use http(s)/ftp to begin with.  Here's the code, (it's
classic ASP as opposed to ASP.net, but you'll do it in a similar way):

httpExpression =
"(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#]
)?"

	Set objRegExpr = New regexp
	objRegExpr.Pattern = httpExpression
	objRegExpr.Global = True
	objRegExpr.IgnoreCase = True
	Set colMatches = objRegExpr.Execute(stringValue)

	For Each Item in colMatches
		stringValue = replace(stringValue, Item.Value, "<link target=""" &
Item.Value & """>" & Item.Value & "</link>")
	Next

Some examples of it's output will be

http://www.anansi.co.uk  will become <link
target="http://www.anansi.co.uk">http://www.anansi.co.uk</link>
http://www.anansi.co.uk/index.html.  will become <link
target="http://www.anansi.co.uk/index.html">http://www.anansi.co.uk/index.ht
ml</link>
http://brett.normanberesford.com will become <link
target="http://brett.normanberesford.com">http://brett.normanberesford.com</
link>
ftp://mog will become <link target="ftp://mog">ftp://mog</link>


To change it to produce <a></a> output you just alter the "stringValue =
replace" line.  The advantage of forcing the user to use http etc is that it
will pick up on URLs that start with something other then www or ftp

HTH

Norman



> -----Original Message-----
> From: thelist-admin at lists.evolt.org
> [mailto:thelist-admin at lists.evolt.org]On Behalf Of Jason Cartwright
> Sent: 04 June 2002 15:55
> To: thelist at lists.evolt.org
> Subject: [thelist] Parsing URLs in ASP.net
>
>
> Does anyone know of any tutorials on parsing URLs using ASP.net (VB
> script)?
>
> I'm looking to take a user's input and convert any urls they enter into
> HTML linking that url.
>
> Eg -
> http://www.foo.com or www.foo.com becomes...
> <a href="http://www.foo.com">http://www.foo.com</a>
>
>
> Thanks!
> Jason
> www.jasoncartwright.com
>
> --
> For unsubscribe and other options, including
> the Tip Harvester and archive of thelist go to:
> http://lists.evolt.org Workers of the Web, evolt !




More information about the thelist mailing list