Regex to find urls in text?

M

Max Williams

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}</a>"}
end

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

thanks
max
 
A

ara.t.howard

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}</a>"}
end

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

thanks
max


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/
 
M

Max Williams

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top