On Tue, Oct 16, 2012 at 9:38 AM, Hans Mackowiak <[email protected]> wrote: > or both regexp combined: > "#hit#here#".gsub(/(^#)|(\#$)/,'') #=> "hit#here"
Few remarks: you do not need the capturing groups. And using \A and \z is better since it will be really the beginning and the end of the string. Consider irb(main):008:0> s = "aaaa\n#bbb#cc#\n" => "aaaa\n#bbb#cc#\n" irb(main):009:0> s.gsub /^#|\#$/, '' => "aaaa\nbbb#cc\n" Note: the backslash before the second # is needed to avoid string interpolation confusion. According to the original specification neither the # before "bbb" has to be removed nor the one after "cc". On the contrary with \A and \z the string remains unchanged which is what should happen: irb(main):010:0> s.gsub /\A#|#\z/, '' => "aaaa\n#bbb#cc#\n" irb(main):011:0> s == s.gsub(/\A#|#\z/, '') => true Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ -- You received this message because you are subscribed to the Google Groups ruby-talk-google group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at https://groups.google.com/d/forum/ruby-talk-google?hl=en
