# Re: Beginner gsub and ri questions

**URL:** <https://www.ruby-forum.com/t/re-beginner-gsub-and-ri-questions/94011>\
**Category:** Ruby\
**Created:** [April 17, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/re-beginner-gsub-and-ri-questions/94011 "2006-04-17T23:14:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [April 17, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/re-beginner-gsub-and-ri-questions/94011/1 "2006-04-17T23:14:23Z")

</div>

Thanks, Austin. I’m posting my current solution below. There’s  
probably a more elegant way of counting the number of replacements using  
the MatchData object or Regexp implicit variables, but I haven’t figured  
that out yet.

Jamal

text = “DOG Dog mouse dog rat dOg”  
find = “dog”  
replace = “cat”  
find = Regexp.new(Regexp.escape(find), Regexp::IGNORECASE)

i = 0  
text = text.gsub(find) {|match|i += 1; replace}  
s = i.to\_s + " replacement"  
s += “s” unless i == 1

puts(text) # cat cat mouse cat rat cat  
puts(s) # 4 replacements
