获取URL从文本文本、URL

2023-09-03 10:22:55 作者:愛、消逝

可能重复:   正则表达式的网址,包括查询字符串

我有一个文本或消息。

  

嘿!试试这个 http://www.test.com/test.aspx?id=53

我们的要求是使用下面的code,以获得从text.We链接

 名单,其中,串>名单=新的名单,其中,串>();
正则表达式urlRx =新
正则表达式(@(小于网址>(HTTP:| HTTPS:[/] [/] | WWW)([AZ] | [AZ] | [0-9] | [/] | [〜])? *),
RegexOptions.IgnoreCase);

MatchCollection匹配= urlRx.Matches(消息);
的foreach(在比赛中赛赛)
{
   list.Add(match.Value);
}
返回列表;
 

它给出​​的网址,但的code不完整的one.Output是

  Flash从文本文件获取数据的详细步骤

http://www.test.com/test.aspx

但是,我们需要完整的URL像

  

http://www.test.com/test.aspx?id=53

请建议如何解决issue.Thanks提前。

解决方案

试试这个正则表达式,返回查询字符串也

(http|ftp|https)://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?

您可以在 gskinner

测试

Possible Duplicate: regex for URL including query string

I have a text or message.

Hey! try this http://www.test.com/test.aspx?id=53

Our requirement is to get link from a text.We are using following code

List<string> list = new List<string>();
Regex urlRx = new
Regex(@"(?<url>(http:|https:[/][/]|www.)([a-z]|[A-Z]|[0-9]|[/.]|[~])*)",
RegexOptions.IgnoreCase);

MatchCollection matches = urlRx.Matches(message);
foreach (Match match in matches)
{
   list.Add(match.Value);
}
return list;

It gives url but not the complete one.Output of the code is

http://www.test.com/test.aspx

But we need complete url like

http://www.test.com/test.aspx?id=53

Please suggest how to resolve that issue.Thanks in advance.

解决方案

Try this regex, returns the query string also

(http|ftp|https)://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?

You can test it on gskinner