Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Regex to find urls in text?
Max Williams
7/11/2008 4:05:00 PM
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
--
Posted via
http://www.ruby-...
.
3 Answers
ara.t.howard
7/11/2008 4:20:00 PM
0
On Jul 11, 2008, at 10:05 AM, Max Williams wrote:
> 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
> --
> Posted via
http://www.ruby-...
.
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://codefor...
|uris
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transi...
http://www.w3.org/...
http://codefor...
/jquery.js
http://drawohara.t...
http://rubyforge.org/projects/cod...
http://ithmez...
http://sciruby.codefor...
http://groups.google.com/group/comp.lang.ruby/search?q=cat+a.rb+howard&start=0&...
(thanks manveru ;-))
a @
http://codefor...
/
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama
Max Williams
7/11/2008 4:22:00 PM
0
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?
--
Posted via
http://www.ruby-...
.
Max Williams
7/11/2008 4:31:00 PM
0
> 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!
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Regex to find urls in text?
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password