Man-with-a-e
Man-with-a-e

Reputation: 87

Extracting HTTP link(http://) for the string in VB.NET

I have the following column values in my table

Sample values:

  1. Check out ABC Group Blog post about XYZ Today! http://blog.saedg.org/

  2. 'Join ABC in urging the county to help create new jobs and economic opportunities for XYZCounty. http://starabcnet.com/news/science/environment/article_4b3d2f7e-96c6-5007-a036-b4613250755e.html

I want to have 2 variables having the links and content separate – example:

  1. Var1 = Check out ABC Group Blog post about XYZ Today!
    Var2 = http://blog.saedg.org/

  2. Var1 = Join ABC in urging the county to help create new jobs and economic opportunities for XYZCounty
    Var2 = http://starabcnet.com/news/science/environment/article_4b3d2f7e-96c6-5007-a036-b4613250755e.html

I guess it can be done via String funcions or regular expression.

Upvotes: 0

Views: 2355

Answers (2)

Stuart Blackler
Stuart Blackler

Reputation: 3772

Try this:

' This is the regex that is used to get the addresses
Public Shared URL As New Regex("(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])", RegexOptions.Compiled)

' URL.Matches returns a collection of results that you can iterate through eg:
for each ele in URL.Matches("INPUT STRING")
    ' do something with each link that you get 
    ' like removing them from the input string
next

Upvotes: 1

Bruno Costa
Bruno Costa

Reputation: 2720

Can you separate the column in two different columns? Like, "Message" and "Link" for example.

If you can not do that, assuming the Http is always in the end of string and there is no other reference to HTTP:// in the string, you can get the string after "HTTP://".

Look at IndexOf and Substring methods of string in VB.NET.

Something like this (I didn't test):

string url = var.SubString(var.IndexOf("http"))

Upvotes: 0

Related Questions