Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recognize URL in plain text

Tags:

regex

url

asp.net

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.

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?

like image 702
gfrizzle Avatar asked Dec 04 '08 20:12

gfrizzle


People also ask

How do I paste a URL into plain text?

Under Settings, select Share, Copy & Paste. Under Share, Copy & Paste, select the default option you want: Link, to paste the URL as a hyperlinked title, or Plain text, to paste the URL as a web address.

Can plain text have hyperlinks?

Here's a few things you should know about plain text emails: You can't control text size to make headings stand out, or emphasize words with bold, italic or colored fonts. It's not possible to include images or use hyperlinks in plain text emails. Only basic reporting is available.

How do I identify a URL?

The website's URL is in the address bar, which is usually at the top of your web browser window. This bar may be at the bottom of the window in Chrome on some Androids. Copy the URL. If you want to paste the URL into a message, post, or another app, you can copy and paste it from the address bar.

How do I find the URL of a string?

php use VStelmakh\UrlHighlight\UrlHighlight; $urlHighlight = new UrlHighlight(); // Extract urls $urlHighlight->getUrls("This is example http://example.com."); // return: ['http://example.com'] // Make urls as hyperlinks $urlHighlight->highlightUrls('Hello, http://example.com.


1 Answers

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+&@#/%=~_|$])
like image 91
MrKurt Avatar answered Sep 17 '22 13:09

MrKurt