From: Robert Klemme Date: 2010-12-09T15:00:18+09:00 Subject: Re: Splitting on capital letters On 09.12.2010 02:18, Sam Duncan wrote: > Very nice =] > > On 09/12/10 14:07, Siep Korteling wrote: >> Sam Duncan wrote in post #967298: >> >>> This might be ridiculous, but ... >>> >>> ("BenefitsAndFeatures".gsub(/[A-Z]/) { |c| "-#{c}" >>> }).reverse.chomp('-').reverse >>> >>> Sam >>> >> Eliminating the reverse.chomp.reverse: >> >> "BenefitsAndFeatures".gsub(/\w(?=[A-Z])/){|match| "#{match}-"} >> >> the (?=[A-Z]) bit is a positive lookahead; the regex matches a word >> character followed by a capital, without making the capital part of the >> match. Easier with lookbehind: irb(main):003:0> s='BenefitsAndFeatures' => "BenefitsAndFeatures" irb(main):004:0> s.gsub(/(?<=[[:lower:]])[[:upper:]]+/, '-\\&') => "Benefits-And-Features" #scan works, too: irb(main):005:0> s.scan(/[[:upper:]]+[[:lower:]]+/).join('-') => "Benefits-And-Features" Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/