2013-05-27 50 views
0

如何从文本中获取URL?如何从文本中获取URL

例如:

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

我一定要得到URL,并将其设置为其他变量。我怎样才能用ASP.NET做到这一点?

+1

使用正则表达式模式 – PiLHA

+3

http://stackoverflow.com/questions/5218863/find-url-in-plain -text-and-insert-html-a-markups接下来做了一些研究 – Liviu

回答

1

添加到以前的答案,你可以这样做,以及:

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; 
1
reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; 

这是从文本中查找URL的正则表达式。

希望它有帮助。

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

MatchCollection _Match = Regex.Matches(_Url , @"http.+)([\s]|$)"); 
string _Address= _Match[0].Value.ToString(); 
1

U可以使用

“/(HTTP | HTTPS | FTP | FTPS)\:// [A-ZA-Z0-9-。] + [A-ZA-Z] {。 2,3}(/ \ S *)/”

为您搜索的正则表达式... ;?)

1
string url = Text.Substring(Text.IndexOf("http://"));