Find URL in a string

M

Mike Kansky

Is there a built in function in .NET to find URL in a string so i can format
it as <a href="url" ?

useful for Forum Posts and other human entered text.
 
S

S. Justin Gengo

Mike,

Regular expressions work well for this.

Something like:

Dim RegEx As New
System.Text.RegularExpressions.Regex("(href|url|src)\s*=\s*(?:(?:\""(?<url>[^\""]*)\"")|(?<url>[^\s*]
))", System.Text.RegularExpressions.RegexOptions.IgnoreCase Or
System.Text.RegularExpressions.RegexOptions.Compiled)

Dim Matches As System.Text.RegularExpressions.MatchCollection =
RegEx.Matches(html)



Regards,


--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
M

Mike Kansky

Killer method i just found:
Imports System.Text.RegularExpressions
Return Regex.Replace(sURL, "(\bhttp://[^ ]+\b)", "<a href=""$0"">$0</a>")

COOL!







S. Justin Gengo said:
Mike,

Regular expressions work well for this.

Something like:

Dim RegEx As New
System.Text.RegularExpressions.Regex("(href|url|src)\s*=\s*(?:(?:\""(?<url>[^\""]*)\"")|(?<url>[^\s*]
))", System.Text.RegularExpressions.RegexOptions.IgnoreCase Or
System.Text.RegularExpressions.RegexOptions.Compiled)

Dim Matches As System.Text.RegularExpressions.MatchCollection =
RegEx.Matches(html)



Regards,


--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche


Mike Kansky said:
Is there a built in function in .NET to find URL in a string so i can
format it as <a href="url" ?

useful for Forum Posts and other human entered text.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top