0

How can I get URL from the text?

For example:

string Text = "this is text with url http://test.com";

I have to get url and set it to other variable. How can I do this with ASP.NET?

lukso
  • 589
  • 5
  • 13
  • 31
  • 3
    http://stackoverflow.com/questions/5218863/find-url-in-plain-text-and-insert-html-a-markups next do a little bit of research – Liviu May 27 '13 at 12:21

6 Answers6

2

String.Split Method (String[], StringSplitOptions)

http://msdn.microsoft.com/en-us/library/tabh47cf.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

You can find examples: C# - C++ - F# - VB

[ComVisibleAttribute(false)]
public string[] Split(
string[] separator,
StringSplitOptions options
)

In this case, "http://" can be a good string for separator.

rnglbd
  • 471
  • 3
  • 6
1
reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

This is the regex to find the URL from Text.

Hope Its Helpful.

Freelancer
  • 9,008
  • 7
  • 42
  • 81
1
string _Url = "this is text with url http://test.com";

MatchCollection _Match = Regex.Matches(_Url , @"http.+)([\s]|$)");
string _Address= _Match[0].Value.ToString();
Kamil T
  • 2,232
  • 1
  • 19
  • 26
1

U can use

"/(http|https|ftp|ftps)\://[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(/\S*)?/"

as regular expression for your search... ;)

Magurizio
  • 300
  • 2
  • 10
1

Adding to the previous answers, you can do this as well:

string text = "this is text with url http://test.com";
Match match = Regex.Match(text, @"http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$");

// gets you http://test.com
string url = match.Value;
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
1
string url = Text.Substring(Text.IndexOf("http://"));
Ted
  • 3,985
  • 1
  • 20
  • 33