识别URL以纯文本 [英] Recognize URL in plain text
问题描述
这应该很容易,但我不知道如何最好地去做。我有一个WinForms应用程序,让长描述的用户类型。 Occaisionally,他们将输入的URL,并在RichTextBox会承认他们,让他们点击时显示。
This should be easy, but I'm not sure how to best go about it. I have a WinForms app that lets the user type in long descriptions. Occaisionally, they would type in URLs, and the RichTextBox would recognize them and make them clickable when displayed.
我在移动应用网络,而我不知道如何使这些相同的URL点击。有一些半自动化的方式来http://www.google.com或www.google.com转换为可点击的链接?我必须诉诸正则表达式匹配?
I'm moving the app to the web, and I'm not sure how to make those same URLs clickable. Is there some semi-automatic way to convert "http://www.google.com" or "www.google.com" to clickable links? Do I have to resort to RegEx matching?
推荐答案
它实际上是一个非常棘手的问题。你可以得到接近,但还不完善,与常规的前pressions。有潜在的正则表达式模式的一个非常好的击穿这里要考虑:的 http://www.regexguru.com/2008/11/detecting-urls-in-a-block-of-text/
It's actually a very difficult problem. You can get close, but not perfect, with regular expressions. There's a very nice breakdown of potential regex patterns to consider here: http://www.regexguru.com/2008/11/detecting-urls-in-a-block-of-text/
最后一个,他列出好像它是件好事足以满足大多数用途:
The last one he lists seems like it's probably good enough for most purposes:
\b(?:(?:https?|ftp|file)://|www\.|ftp\.)
(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*
(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$])
这篇关于识别URL以纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!