用于链接字符串中的 url 的 C# 代码
有没有人有任何好的 C# 代码(和正则表达式)来解析字符串并“链接”字符串中可能存在的任何 url?
Does anyone have any good c# code (and regular expressions) that will parse a string and "linkify" any urls that may be in the string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是一个非常简单的任务,您可以使用 来完成它正则表达式 和现成的正则表达式来自:
类似:
您可能不仅对创建链接感兴趣,而且对缩短 URL 感兴趣。 这是关于此主题的一篇好文章:
另请参阅:
It's a pretty simple task you can acheive it with Regex and a ready-to-go regular expression from:
Something like:
You may also be interested not only in creating links but in shortening URLs. Here is a good article on this subject:
See also:
好吧,经过对此进行了大量研究,并多次尝试修复
我们现在正在使用这个 HtmlHelper 扩展...我想我会分享并获得任何评论:
well, after a lot of research on this, and several attempts to fix times when
we are now using this HtmlHelper extension... thought I would share and get any comments:
这并不像您在 Jeff Atwood 的博客文章中读到的那么容易。 检测 URL 的结束位置尤其困难。
例如,尾部括号是否是 URL 的一部分:
在第一个中在这种情况下,括号是 URL 的一部分。 在第二种情况下,他们不是!
It's not that easy as you can read in this blog post by Jeff Atwood. It's especially hard to detect where an URL ends.
For example, is the trailing parenthesis part of the URL or not:
In the first case, the parentheses are part of the URL. In the second case they are not!
发现以下正则表达式
http://daringfireball.net/2010/07/improved_regex_for_matching_urls
对我来说看起来非常好。 Jeff Atwood 解决方案不能处理很多情况。 josefresno 在我看来处理所有案例。 但当我试图理解它时(如果有任何支持请求),我的大脑就沸腾了。
Have found following regular expression
http://daringfireball.net/2010/07/improved_regex_for_matching_urls
for me looks very good. Jeff Atwood solution doesn't handle many cases. josefresno seem to me handle all cases. But when I have tried to understand it (in case of any support requests) my brain was boiled.
有类:
在此方法中使用它:
测试用例:
There is class:
Use it in this method:
Test cases:
这对我有用:
This works for me: