Regular Expressions

Does anyone know how can I replace all http://example.com to http://example.com in text?

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 “” 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 http://example.com in text?


Rails mailing list
[email protected]
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!

Assuming your text is in a string called ‘text’, you just need to
perform some regexp-fu!

result = text.gsub(/http://\S+/, ‘\0’)

Cheers!

-DF

On Nov 29, 2005, at 1:33 PM, Dmytro wrote:

Does anyone know how can I replace all http://example.com to http://example.com in text?


Rails mailing list
[email protected]
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| “#
{url}
”}
p text
#=> “http://example.com what ever man
\n another junk word http://
www.example2.com

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
[email protected]

me wrote:

Does anyone know how can I replace all http://example.com to http://example.com in text?

Maybe you can try auto_link helper
http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000419

On Tue, Nov 29, 2005 at 09:33:35PM +0000, Dmytro wrote:

Does anyone know how can I replace all http://example.com to http://example.com in text?

sed ‘s%http://example.com%http://example.com%’ < 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 ‘\1’.

Ruby-wise, that should reduce to:

output = input.gsub(/(http://[a-zA-Z/.?;%]+)/, ‘\1’)

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