如何匹配c#中的URL ?

时间:2022-05-12 23:35:29

I have found many examples of how to match particular types of URL-s in PHP and other languages. I need to match any URL from my C# application. How to do this? When I talk about URL I talk about links to any sites or to files on sites and subdirectiories and so on.

我找到了许多示例,说明如何在PHP和其他语言中匹配特定类型的URL-s。我需要匹配来自c#应用程序的任何URL。如何做到这一点呢?当我谈到URL时,我指的是指向任何网站或网站上的文件和子目录等的链接。

I have a text like this: "Go to my awsome website http:\www.google.pl\something\blah\?lang=5" or else and I need to get this link from this message. Links can start only with www. too.

我有一个这样的短信:“去我的awsome网站http:\www.google.pl\什么?”lang=5"或其他,我需要从这个消息中得到这个链接。链接只能从www开始。了。

6 个解决方案

#1


14  

If you need to test your regex to find URLs you can try this resource

如果需要测试regex以找到url,可以尝试此资源

http://gskinner.com/RegExr/

http://gskinner.com/RegExr/

It will test your regex while you're writing it.

它将在您编写regex时测试它。

In C# you can use regex for example as below:

在c#中,您可以使用regex,例如如下:

Regex r = new Regex(@"(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*");
// Match the regular expression pattern against a text string.
Match m = r.Match(text);
while (m.Success) 
{
   //do things with your matching text 
   m = m.NextMatch();
}

#2


15  

Microsoft has a nice page of some regular expressions...this is what they say (works pretty good too)

微软有一个很好的页面,有一些正则表达式……这是他们说的(效果也不错)

^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$

http://msdn.microsoft.com/en-us/library/ff650303.aspx#paght000001_commonregularexpressions

http://msdn.microsoft.com/en-us/library/ff650303.aspx paght000001_commonregularexpressions

#3


5  

I am not sure exactly what you are asking, but a good start would be the Uri class, which will parse the url for you.

我不确定您要问什么,但是一个好的开始是Uri类,它将为您解析url。

#4


5  

Here's one defined for URL's.

这是为URL定义的。

^http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$

http://msdn.microsoft.com/en-us/library/ms998267.aspx

http://msdn.microsoft.com/en-us/library/ms998267.aspx

#5


1  

Regex regx = new Regex("http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase); 

#6


0  

This will return a match collection of all matches found within "yourStringThatHasUrlsInIt":

这将返回在“yourStringThatHasUrlsInIt”中找到的所有匹配集合:

var pattern = @"((ht|f)tp(s?)\:\/\/|~/|/)?([w]{2}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?";
var regex = new Regex(pattern);
var matches = regex.Matches(yourStringThatHasUrlsInIt);

The return will be a "MatchCollection" which you can read more about here:

返回将是一个“MatchCollection”,您可以在这里阅读更多:

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchcollection.aspx

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchcollection.aspx

#1


14  

If you need to test your regex to find URLs you can try this resource

如果需要测试regex以找到url,可以尝试此资源

http://gskinner.com/RegExr/

http://gskinner.com/RegExr/

It will test your regex while you're writing it.

它将在您编写regex时测试它。

In C# you can use regex for example as below:

在c#中,您可以使用regex,例如如下:

Regex r = new Regex(@"(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*");
// Match the regular expression pattern against a text string.
Match m = r.Match(text);
while (m.Success) 
{
   //do things with your matching text 
   m = m.NextMatch();
}

#2


15  

Microsoft has a nice page of some regular expressions...this is what they say (works pretty good too)

微软有一个很好的页面,有一些正则表达式……这是他们说的(效果也不错)

^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$

http://msdn.microsoft.com/en-us/library/ff650303.aspx#paght000001_commonregularexpressions

http://msdn.microsoft.com/en-us/library/ff650303.aspx paght000001_commonregularexpressions

#3


5  

I am not sure exactly what you are asking, but a good start would be the Uri class, which will parse the url for you.

我不确定您要问什么,但是一个好的开始是Uri类,它将为您解析url。

#4


5  

Here's one defined for URL's.

这是为URL定义的。

^http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$

http://msdn.microsoft.com/en-us/library/ms998267.aspx

http://msdn.microsoft.com/en-us/library/ms998267.aspx

#5


1  

Regex regx = new Regex("http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase); 

#6


0  

This will return a match collection of all matches found within "yourStringThatHasUrlsInIt":

这将返回在“yourStringThatHasUrlsInIt”中找到的所有匹配集合:

var pattern = @"((ht|f)tp(s?)\:\/\/|~/|/)?([w]{2}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?";
var regex = new Regex(pattern);
var matches = regex.Matches(yourStringThatHasUrlsInIt);

The return will be a "MatchCollection" which you can read more about here:

返回将是一个“MatchCollection”,您可以在这里阅读更多:

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchcollection.aspx

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchcollection.aspx