Forum: Ruby on Rails Regular Expressions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
me (Guest)
on 2005-11-29 23:42
(Received via mailing list)
Does anyone know how can I replace all http://example.com to <a
href="http://example.com">http://example.com</a> in text?
mailing (Guest)
on 2005-11-30 00:42
(Received via mailing list)
Dmytro,

I'm not a Ruby guru yet, so sorry that it's in perl (maybe a Ruby master
can
deliver the syntax).

First make sure the initial url has it's special characters escaped
	$escaped_url = regex.escape($url);

search and replace the url with the full link. The $1 is a so called
backreference to whats between the brackets "(" and ")".

	$html_anchor =~ s/($escaped_url)/<a href="$1">$1<\/a>/g

(As you can see the "</a>" is placed as "<\/a>" the slash is a special
character and therefore escaped with the "\"

I've tried it and it works. There's only one 'but'. The url fed into the
regex
is not checked on valid syntax yet.

Hope this helps.

Regards,

Gerard.

On Tuesday 29 November 2005 22:33, Dmytro tried to type something like:
> Does anyone know how can I replace all http://example.com to <a
> href="http://example.com">http://example.com</a> in text?
>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails

--
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!
ezra (Guest)
on 2005-11-30 00:46
(Received via mailing list)
On Nov 29, 2005, at 1:33 PM, Dmytro wrote:

> Does anyone know how can I replace all http://example.com to <a
> href="http://example.com">http://example.com</a> in text?
>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

Sure try this:

text = "http://example.com what ever man\n another junk word http://
www.example2.com"
text.gsub!(/(http:\/\/\w*\.*\w+\.\w+\b)/){|url| "<a href='#{url}'>#
{url}</a>"}
p text
#=> "<a href='http://example.com'>http://example.com</a> what ever man
\n another junk word <a href='http://www.example2.com'>http://
www.example2.com</a>"


That will do a substitution on all url's in your text. It deals with
http://example.com and http://www.example.com. You will have to tweak
the regex to get more than one subdomain to work though.

Enjoy-

-Ezra Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid
david.felstead (Guest)
on 2005-11-30 00:50
(Received via mailing list)
Assuming your text is in a string called 'text', you just need to
perform some regexp-fu!

result = text.gsub(/http:\/\/\S+/, '<a href="\0">\0</a>')

Cheers!

-DF
Joon L. (Guest)
on 2005-11-30 00:58
me wrote:
> Does anyone know how can I replace all http://example.com to <a
> href="http://example.com">http://example.com</a> in text?

Maybe you can try auto_link helper
http://api.rubyonrails.com/classes/ActionView/Help...
mpalmer (Guest)
on 2005-11-30 01:55
(Received via mailing list)
On Tue, Nov 29, 2005 at 09:33:35PM +0000, Dmytro wrote:
> Does anyone know how can I replace all http://example.com to <a
> href="http://example.com">http://example.com</a> in text?

sed 's%http://example.com%<a
href="http://example.com">http://example.com</a>%'... < input > output

Of course, you may be looking for something more general, in which case
you
can specify a more complex regular expression (one which matches
whatever
you're actually looking for, such as a general URL) inside parentheses,
and
then replace with '<a href="\1">\1</a>'.

Ruby-wise, that should reduce to:

output = input.gsub(/(http:\/\/[a-zA-Z\/.?;%]+)/, '<a href="\1">\1</a>')

Caveat: The regular expression in there is *guaranteed* to be
incomplete; I
just gave it as an example (which happens to match the example you gave,
but
not necessarily much more).

A more complete regexp can be found at
google://valid+url+regular+expression/, but be careful: not all of the
examples you might find are equally correct...

- Matt

--
"[the average computer user] has been served so poorly that he expects
his
system to crash all the time, and we witness a massive worldwide
distribution of bug-ridden software for which we should be deeply
ashamed."
		-- Edsger Dijkstra
This topic is locked and can not be replied to.