Regex to find urls in text?

On our site, our resources have a description, which is often copied and
pasted from somewhere else. Often these descriptions have urls in them,
just as normal text, and i’d like to automatically make these into
working links.

I was thinking of using gsub as below:

def description_with_links
regex = ???
self.description.gsub(regex) {|url|"<a href=’#{url}’>#{url}"}
end

The above should work ok, once i have the regex but i can’t work it out.
Can anyone help?

thanks
max

On Jul 11, 2008, at 10:05 AM, Max W. wrote:

regex = ???
Posted via http://www.ruby-forum.com/.
cfp:~ > cat bin/uris
#! /usr/bin/env ruby

require ‘uri’; protocols = %w[http ftp]; while line = gets;
protocols.map{|pr| URI::extract(line, pr) }.flatten.compact.each{|uri|
puts uri }; end

cfp:~ > curl -s http://codeforpeople.com|uris
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
http://www.w3.org/1999/xhtml
http://codeforpeople.com/jquery.js
http://drawohara.tumblr.com/
http://rubyforge.org/projects/codeforpeople
http://ithmezipper.net/
http://sciruby.codeforpeople.com
http://groups.google.com/group/comp.lang.ruby/search?q=cat+a.rb+howard&start=0&scoring=d

(thanks manveru ;-))

a @ http://codeforpeople.com/

Actually this seems to work (for a couple of examples):

regex = /(http://|www.)([\w-]+.)+[\w-]+/

Can anyone see any flaws, or know of a better version?

cfp:~ > cat bin/uris
#! /usr/bin/env ruby

require ‘uri’; protocols = %w[http ftp]; while line = gets;
protocols.map{|pr| URI::extract(line, pr) }.flatten.compact.each{|uri|
puts uri }; end

aha, thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs