From: Robert Klemme Date: 2010-03-01T23:48:36+09:00 Subject: Re: gsub for string 2010/3/1 Brian Candler : > Robert Klemme wrote: >> You can match arbitrary characters and use capturing groups >> >> str.gsub(/--([^-]+)--/, '-\\1--1') > > which you can also do in a block form, which is sometimes clearer and > lets you do things like modifying xxxx. > > str.gsub(/--([^-]+)--/) { "-#{$1}--1" } For fairness reasons you should also mention that this is also slower and not needed in this particular case since the replacement is fixed. :-) This is also a good opportunity to link to my most recent blog entry which dealt with string replacements: http://blog.rubybestpractices.com/posts/rklemme/020-Code_Massage.html Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/