# Problem with my gsub!

**URL:** <https://www.ruby-forum.com/t/problem-with-my-gsub/60647>\
**Category:** Ruby\
**Created:** [May 16, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/problem-with-my-gsub/60647 "2006-05-16T14:56:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 16, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/problem-with-my-gsub/60647/1 "2006-05-16T14:56:25Z")

</div>

I can’t get a gsub! to work when I use a counter variable in my  
replacement.

content.gsub!(/^%%Page:.\*([0-9]{1,5})$/, “%%Page: $1”)

I literally get “Page: $1” in my output. Why? How do I indicate that I  
want the number, whatever it was, from the search? I’ve tried #{$1}, but  
that didn’t work either. RUBY does use $1 for its counters, doesn’t it?  
I’ve tried \1, but, that gives me even worse results.

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 16, 2006, 3:01pm UTC](https://www.ruby-forum.com/t/problem-with-my-gsub/60647/2 "2006-05-16T15:01:58Z")

</div>

> I’ve tried \1, but, that gives me even worse results.

‘%%Page: \1’ or “%%Page: \1” does what you want.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 16, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/problem-with-my-gsub/60647/3 "2006-05-16T15:23:56Z")

</div>

Christoffer S. wrote:

> > I’ve tried \1, but, that gives me even worse results.
> 
> ‘%%Page: \1’ or “%%Page: \1” does what you want.

Thank you, Christopher!

Now, could you tell me how to have a counter for that \1? In other  
words, I want the numbers to increment, starting from 1 through however  
many “%%Page” indicators there are. Is there any way to do that in the  
same gsub! line?

Thanks again.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 16, 2006, 3:33pm UTC](https://www.ruby-forum.com/t/problem-with-my-gsub/60647/4 "2006-05-16T15:33:16Z")

</div>

On 5/16/06, Guest [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> same gsub! line?
> 
> Thanks again.

The block form of gsub is one way to go about it:  
counter = 0  
content.gsub!(/^%%Page:.\*([0-9]{1,5})$/) do |match|  
counter += 1  
“%%Page: #{counter}”  
end

Whatever the block returns is used to replace the matched text.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 16, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/problem-with-my-gsub/60647/5 "2006-05-16T15:35:41Z")

</div>

Wilson B. wrote:

> On 5/16/06, Guest [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > same gsub! line?
> > 
> > Thanks again.
> 
> The block form of gsub is one way to go about it:  
> counter = 0  
> content.gsub!(/^%%Page:.\*([0-9]{1,5})$/) do |match|  
> counter += 1  
> “%%Page: #{counter}”  
> end
> 
> Whatever the block returns is used to replace the matched text.

Cool. Thank you both very much. This is a very generous forum, I must  
say.

-Peter

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 16, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/problem-with-my-gsub/60647/6 "2006-05-16T21:45:19Z")

</div>

On 5/16/06, Guest [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > counter += 1  
> > “%%Page: #{counter}”  
> > end
> > 
> > Whatever the block returns is used to replace the matched text.
> 
> Cool. Thank you both very much. This is a very generous forum, I must  
> say.

I wrote that reply prior to coffee, and you’ll note that the |match|  
block variable is unnecessary, since you’re not doing anything with  
it.  
Also, I meant to say “the value of the block”, not “what the block  
returns”.  
Other than that, it stands up to 6 hours of wakefulness. Heh.
