From: Jamal Mazrui Date: 2006-04-18T06:13:01+09:00 Subject: Re: Beginner gsub and ri questions 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 -----Original Message----- From: Austin Ziegler [mailto:halostatue@gmail.com] Sent: Monday, April 17, 2006 1:45 PM To: ruby-talk ML Subject: Re: Beginner gsub and ri questions On 4/17/06, Jamal Mazrui wrote: > Thanks for the response. Is there a way to do this without a regular > expression? To further explain, I want the program to be able to do any > kind of literal string replacement at runtime, except that case is > ignored. If I use a regular expression as the match string, then > certain characters could be interpreted in special, non-literal ways, > which is not what I want in this case. What you want here is to construct your regular expression properly, such as: catmatch = %r{#{Regexp::escape(cat)}}i stringval =~ catmatch The Regexp::escape (ri it) will prevent your fears from coming true, here. -austin -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca