# Substituting with a find counter . .

**URL:** <https://www.ruby-forum.com/t/substituting-with-a-find-counter/61373>\
**Category:** Ruby\
**Created:** [May 23, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/substituting-with-a-find-counter/61373 "2006-05-23T16:01:20Z")\
**Posts on this page:** 3\
**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 23, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/substituting-with-a-find-counter/61373/1 "2006-05-23T16:01:20Z")

</div>

In this gsub block, I’d like to save the searched parenthetical data,  
indicated by (.\*) in line 2. The script works, but, instead of getting  
the parenthetical data, I’m getting “$1.” Very literal, but, not what I  
want. What am I doing wrong?

Thanks.  
Peter

1 counter = 0  
2 contents.gsub!(/^%%Page:(.\*)[0-9]{1,5}$/) do |match|  
3 counter += 1  
4 “%%Page: $1 #{counter}”

---

<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 23, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/substituting-with-a-find-counter/61373/2 "2006-05-23T16:16:03Z")

</div>

2006/5/23, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> In this gsub block, I’d like to save the searched parenthetical data,  
> indicated by (.\*) in line 2. The script works, but, instead of getting  
> the parenthetical data, I’m getting “$1.” Very literal, but, not what I  
> want. What am I doing wrong?

You’re missing a hash which leads to your $1 being used literally:

> 1 counter = 0  
> 2 contents.gsub!(/^%%Page:(.\*)[0-9]{1,5}$/) do |match|  
> 3 counter += 1  
> 4 “%%Page: $1 #{counter}”

make that last line

“%%Page: #$1 #{counter}”

Kind regards

robert

---

<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 23, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/substituting-with-a-find-counter/61373/3 "2006-05-23T16:23:13Z")

</div>

Peter B. wrote:

> In this gsub block, I’d like to save the searched parenthetical data,  
> indicated by (.\*) in line 2. The script works, but, instead of getting  
> the parenthetical data, I’m getting “$1.” Very literal, but, not what I  
> want. What am I doing wrong?
> 
> Thanks.  
> Peter
> 
> 1 counter = 0  
> 2 contents.gsub!(/^%%Page:(.\*)[0-9]{1,5}$/) do |match|  
> 3 counter += 1  
> 4 “%%Page: $1 #{counter}”

“%%Page: #{$1} #{counter}”

should help.

Also, the .\* is gobbling up any digits except the last. You probably  
want a space between .\* and [0-9], or make the . match non-greedy with  
.\*?. Since your replacement string has a space, I’m guessing that’s  
what you want.

– Jim W.
