[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regex back reference in gsub

Dan Fitzpatrick

7/13/2005 5:28:00 PM

I am converting URLs in a text file to hyperlinks with the following
regex. But the results only show up after the gsub is run a second time.

str = "A link to http://ruby-lang...
str.gsub(/([\s|\>|\[|\(])((ftp:\/\/|http(s?):\/\/))([\w\.\?\/&=\-~:%]+)\b/i,
"#{$1}<a href='#{$3}#{$5}#{$6}' target='_blank'>#{$5}#{$6}</a>")
#=> "A link to<a href='' target='_blank'></a>"
str.gsub(/([\s|\>|\[|\(])((ftp:\/\/|http(s?):\/\/))([\w\.\?\/&=\-~:%]+)\b/i,
"#{$1}<a href='#{$3}#{$5}#{$6}' target='_blank'>#{$5}#{$6}</a>")
#=> "A link to <a href='http://ruby-lan...
target='_blank'>ruby-lang.org</a>"

Is there another way to do this?



4 Answers

daz

7/13/2005 7:12:00 PM

0


Dan Fitzpatrick wrote:
> I am converting URLs in a text file to hyperlinks with the following
> regex. But the results only show up after the gsub is run a second time.
>
> str = "A link to http://ruby-lang...
> str.gsub(/([\s|\>|\[|\(])((ftp:\/\/|http(s?):\/\/))([\w\.\?\/&=\-~:%]+)\b/i,
> "#{$1}<a href='#{$3}#{$5}#{$6}' target='_blank'>#{$5}#{$6}</a>")
> #=> "A link to<a href='' target='_blank'></a>"
> str.gsub(/([\s|\>|\[|\(])((ftp:\/\/|http(s?):\/\/))([\w\.\?\/&=\-~:%]+)\b/i,
> "#{$1}<a href='#{$3}#{$5}#{$6}' target='_blank'>#{$5}#{$6}</a>")
> #=> "A link to <a href='http://ruby-lan...
> target='_blank'>ruby-lang.org</a>"
>
> Is there another way to do this?
>

Hi,

Just a small change.

You need to use the block form of gsub, here, because the $n backrefs
are not set until the regex has completed. When using the non-block
form, you can normally use \1, \2 etc. instead of $1, $2 etc. but,
because you're using interpolation with #{...} inside your replacement
string, it gets a bit tricky doubling up the backslashes.

That's really not clear at all, sorry :-)

The reason why your example took two runs, is because the first run
sets the $n backrefs exactly the way you want them for the second run.

#--------------------------------------
str = "A link to http://ruby-lang...

res = str.gsub(/([\s|\>|\[|\(])((ftp:\/\/|http(s?):\/\/))([\w\.\?\/&=\-~:%]+)\b/i) do
"#{$1}<a href='#{$3}#{$5}#{$6}' target='_blank'>#{$5}#{$6}</a>"
end

puts res #-> A link to <a href='http://ruby-lan... target='_blank'>ruby-lang.org</a>
#--------------------------------------


daz



Dan Fitzpatrick

7/13/2005 7:37:00 PM

0

> --snip--
> #--------------------------------------
> str = "A link to http://ruby-lang...
>
> res =
str.gsub(/([\s|\>|\[|\(])((ftp:\/\/|http(s?):\/\/))([\w\.\?\/&=\-~:%]+)\b/i)
do
> "#{$1}<a href='#{$3}#{$5}#{$6}' target='_blank'>#{$5}#{$6}</a>"
> end
>
> puts res #-> A link to <a href='http://ruby-lan...
target='_blank'>ruby-lang.org</a>
> #--------------------------------------

daz,

Thanks for the explanation. That works great.

Dan


daz

7/13/2005 7:44:00 PM

0


Dan Fitzpatrick wrote:
>
> [...]

This is reworked with the backslash backrefs I mentioned:

res = str.gsub(/([\s|\>|\[|\(])((ftp:\/\/|http(s?):\/\/))([\w\.\?\/&=\-~:%]+)\b/i,
'\1<a href="\3\5\6" target="_blank">\5\6</a>')


> Thanks for the explanation. That works great.
>
> Dan
>

Y'welcome,

daz


dblack

7/13/2005 10:32:00 PM

0