[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Gsup with variable when replacing

touffik@gmail.com

7/28/2008 12:43:00 PM

Hi people,

I'm trying to transform this kind of line :
123232321
FOOFOOFOO
...
2431232
BARBARBAR

to
<number>123232321</number> (actually XML)
<number>2431232</number>
So I have coded this script
File.open("D:/digisoft/1b.txt","r") do |f2|
while line = f2.gets
f3 = line.gsub!(/\d+$/,'<number>\1</number>')
puts f3
end
end

but Ruby doesn't seem to apraciate .. I search example and I found
that variable as $1, \1, &$ could do this job but i can't get it
works.
How can I get the match of my regexp and add <number> blocks please ?

thanks you
3 Answers

Sebastian Hungerecker

7/28/2008 12:52:00 PM

0

Romain LOMBARDO wrote:
> f3 = line.gsub!(/\d+$/,'<number>\1</number>')

\1 means "first group", but there are no groups in your regex. \0 means "whole
match", so that's the one you want.
Another thing: gsub! is the mutating variant of gsub. It will return nil when
no change occurs or self when there is a change. Either way it does not make
sense to store its return value.
And yet another thing: there is no sense in using gsub (as opposed to sub)
with a regex that only matches once anyway.

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

sathyz

7/29/2008 6:54:00 AM

0

On Jul 28, 5:42 pm, Romain LOMBARDO <touf...@gmail.com> wrote:
> Hi people,
>
> I'm trying to transform this kind of line :
> 123232321
> FOOFOOFOO
> ..
> 2431232
> BARBARBAR
>
> to
> <number>123232321</number>  (actually XML)
> <number>2431232</number>
> So I have coded this script
> File.open("D:/digisoft/1b.txt","r") do |f2|
>   while line = f2.gets
>        f3 = line.gsub!(/\d+$/,'<number>\1</number>')
>        puts f3
>   end
> end
>
while line = f2.gets
print '<number>',line.to_i,'</number>',"\n" if line=~/\d/
end

- I am not sure if this is perfect solution, but this works and prints
what you expected.

Thanks,
sathyz

Robert Klemme

8/2/2008 8:53:00 AM

0

On 28.07.2008 14:51, Sebastian Hungerecker wrote:
> Romain LOMBARDO wrote:
>> f3 = line.gsub!(/\d+$/,'<number>\1</number>')
>
> \1 means "first group", but there are no groups in your regex. \0 means "whole
> match", so that's the one you want.

I wasn't aware that \0 does the job as well. I always use \& for the
whole match. Learn something new every day. :-)

> Another thing: gsub! is the mutating variant of gsub. It will return nil when
> no change occurs or self when there is a change. Either way it does not make
> sense to store its return value.
> And yet another thing: there is no sense in using gsub (as opposed to sub)
> with a regex that only matches once anyway.

Absolutely.

Another remark, iterating through a file can be made easier:

File.foreach("D:/digisoft/1b.txt") do |line|
line.gsub!(/\d+$/,'<number>\\&</number>')
puts line
end

Kind regards

robert