Problem with my gsub!

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.

···

--
Posted via http://www.ruby-forum.com/.

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

'%%Page: \1' or "%%Page: \\1" does what you want.

···

--
Christoffer Sawicki

Christoffer Sawicki 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.

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

On 5/16/06, Guest <pbailey@bna.com> wrote:

Christoffer Sawicki 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.

Wilson Bilkovich wrote:

···

On 5/16/06, Guest <pbailey@bna.com> 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

--
Posted via http://www.ruby-forum.com/\.

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.

···

On 5/16/06, Guest <pbailey@bna.com> wrote:

Wilson Bilkovich wrote:
> On 5/16/06, Guest <pbailey@bna.com> 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.