On Tue, Oct 16, 2012 at 9:38 AM, Hans Mackowiak <[email protected]> wrote:
> or both regexp combined:
> "#hit#here#".gsub(/(^#)|(\#$)/,'')  #=> "hit#here"

We can also combine by positively matching:

irb(main):001:0> s = "#hit#here#"
=> "#hit#here#"
irb(main):002:0> s = s[/^#?(.*?)#?\z/, 1]
=> "hit#here"

All possible combinations

irb(main):003:0> %w{hi#there #hi#there hi#there# #hi#there#}.each {|s|
p s[/^#?(.*?)#?\z/, 1]}
"hi#there"
"hi#there"
"hi#there"
"hi#there"
=> ["hi#there", "#hi#there", "hi#there#", "#hi#there#"]

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

Reply via email to