C# 从文本中获取url

C# 从文本中获取url,c#,asp.net,.net,regex,C#,Asp.net,.net,Regex,可能重复: 我有一条短信 嘿!试试这个 我们的要求是从文本中获取链接 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.Matche

可能重复:

我有一条短信

嘿!试试这个

我们的要求是从文本中获取链接

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;
List List=新列表();
Regex urlRx=new
正则表达式(@“(?(http:| https:[/][/]]www.)([a-z][a-z][0-9].[/.].[~]*)”,
RegexOptions.IgnoreCase);
MatchCollection matches=urlRx.matches(消息);
foreach(匹配中的匹配)
{
list.Add(match.Value);
}
退货清单;
它给出了url,但不是完整的url

但我们需要完整的网址像


请建议如何解决此问题。请提前感谢。

尝试此正则表达式,并返回查询字符串

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

你可以在

上测试它。如果你以后在代码上使用这个URL(提取一个部分,QueQuin字符串等),请考虑使用

类与助手组合


它可以帮助您完成这些操作。

似乎有点过于明确。
(ftp | https?)://[^\s]+
行得通吗?+1@zapthedingbat这也行得通检查完整的正则表达式,以查找正则文本中隐藏的URL。如果你需要更简单的东西,我认为它的评论足够好,你应该能够适应你的具体情况。看看这个问题,我相信它会解决你的问题。
public List<string> GetLinks(string message)
{
    List<string> list = new List<string>();
    Regex urlRx = new Regex(@"((https?|ftp|file)\://|www.)[A-Za-z0-9\.\-]+(/[A-Za-z0-9\?\&\=;\+!'\(\)\*\-\._~%]*)*", RegexOptions.IgnoreCase);

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

var list = GetLinks("Hey yo check this: http://www.google.com/?q=stackoverflow and this: http://www.mysite.com/?id=10&author=me");
http:// ...
https:// ...
file:// ...
www. ...
Uri uri;
String strUrl = "http://www.test.com/test.aspx?id=53";
bool isUri = Uri.TryCreate(strUrl, UriKind.RelativeOrAbsolute, out uri);
if(isUri){
    Console.WriteLine(uri.PathAndQuery.ToString());
}else{
    Console.WriteLine("invalid");
}