# Gsup with variable when replacing

**URL:** <https://www.ruby-forum.com/t/gsup-with-variable-when-replacing/144307>\
**Category:** Ruby\
**Created:** [July 28, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/gsup-with-variable-when-replacing/144307 "2008-07-28T14:45:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Romain\_LOMBARDO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@Romain\_LOMBARDO](https://www.ruby-forum.com/u/Romain_LOMBARDO)\
**Post date:** [July 28, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/gsup-with-variable-when-replacing/144307/1 "2008-07-28T14:45:52Z")

</div>

Hi people,

I’m trying to transform this kind of line :  
123232321  
FOOFOOFOO  
…  
2431232  
BARBARBAR

to  
123232321 (actually XML)  
2431232  
So I have coded this script  
File.open(“D:/digisoft/1b.txt”,“r”) do |f2|  
while line = f2.gets  
f3 = line.gsub!(/\d+$/,’\1’)  
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 blocks please ?

thanks you

---

<div class="post-metadata">

**Author:** ![Romain\_LOMBARDO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@Romain\_LOMBARDO](https://www.ruby-forum.com/u/Romain_LOMBARDO)\
**Post date:** [July 28, 2008, 2:52pm UTC](https://www.ruby-forum.com/t/gsup-with-variable-when-replacing/144307/2 "2008-07-28T14:52:55Z")

</div>

Romain LOMBARDO wrote:

> f3 = line.gsub!(/\d+$/,’\1’)

\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

---

<div class="post-metadata">

**Author:** ![Romain\_LOMBARDO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@Romain\_LOMBARDO](https://www.ruby-forum.com/u/Romain_LOMBARDO)\
**Post date:** [July 28, 2008, 2:52pm UTC](https://www.ruby-forum.com/t/gsup-with-variable-when-replacing/144307/3 "2008-07-28T14:52:20Z")

</div>

On Mon Jul 28 21:44:41 2008, Romain LOMBARDO wrote:

> 123232321 (actually XML)  
> 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 blocks please ?
> 
> thanks you

Youâ€™re missing the brackets around the \d+, like so:

f3 = line.gsub(/(\d+)$/,’\1’)

Note that I am also using String#gsub not String#gsub! as that will  
change your line variable also.

---

<div class="post-metadata">

**Author:** ![Romain\_LOMBARDO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@Romain\_LOMBARDO](https://www.ruby-forum.com/u/Romain_LOMBARDO)\
**Post date:** [August 2, 2008, 10:55am UTC](https://www.ruby-forum.com/t/gsup-with-variable-when-replacing/144307/4 "2008-08-02T10:55:21Z")

</div>

On 28.07.2008 14:51, Sebastian H. wrote:

> Romain LOMBARDO wrote:
> 
> > f3 = line.gsub!(/\d+$/,’\1’)
> 
> \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+$/,’\&’)  
puts line  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Romain\_LOMBARDO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@Romain\_LOMBARDO](https://www.ruby-forum.com/u/Romain_LOMBARDO)\
**Post date:** [July 29, 2008, 8:55am UTC](https://www.ruby-forum.com/t/gsup-with-variable-when-replacing/144307/5 "2008-07-29T08:55:59Z")

</div>

On Jul 28, 5:42 pm, Romain LOMBARDO [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

while line = f2.gets  
print ‘’,line.to\_i,‘’,“\n” if line=~/\d/  
end

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

Thanks,  
sathyz
