# Splitting on capital letters

Assume I have camelized string like
BenefitsAndFeatures
and I want to convert that to
Benefits-And-Features

What is a good “Ruby way” to do that?

On 12/08/2010 05:47 PM, Ralph S. wrote:

Assume I have camelized string like
BenefitsAndFeatures
and I want to convert that to
Benefits-And-Features

What is a good “Ruby way” to do that?

Whether or not this is a good way may be up for debate, but it’s a
way:

irb(main):001:0>
“BenefitsAndFeatures”.split(/([[:upper:]][[:lower:]]*)/).delete_if(&:empty?).join("-")
=> “Benefits-And-Features”

JB> Whether or not this is a good way may be up for debate, but it’s a
way:

irb(main):001:0>>
“BenefitsAndFeatures”.split(/([[:upper:]][[:lower:]]*)/).delete_if(&:empty?).join("-")
=>> “Benefits-And-Features”

This is sooo far beyond my skill level.

I’m gonna have to study this one for a while.

Thanks!

Ralph

Here’s an ActiveSupport-powered roflscale method:

“BenefitsAndFeatures”.underscore.split(’_’).map(&:capitalize).join(’-’)
=> “Benefits-And-Features”

This might be ridiculous, but …

(“BenefitsAndFeatures”.gsub(/[A-Z]/) { |c| “-#{c}”
}).reverse.chomp(’-’).reverse

Sam

On 12/08/2010 06:02 PM, Ralph S. wrote:

Ralph
Simple explanation:

“BenefitsAndFeatures”.split(/([[:upper:]][[:lower:]])/) returns an
array that was made from the string provided. The pattern for splitting
is a capital letter ([[:upper:]]) followed by one or more lowercase
letters ([[:lower:]]
):

irb(main):001:0>
“BenefitsAndFeatures”.split(/([[:upper:]][[:lower:]]*)/)
=> ["", “Benefits”, “”, “And”, “”, “Features”]
irb(main):002:0>

On that array, we call .delete_if(&:empty?) which will delete any
element from the array if it returns true to the empty check.

irb(main):002:0> _.delete_if(&:empty?)
=> [“Benefits”, “And”, “Features”]
irb(main):003:0>

On the cleaned array, we re-join into a string putting a dash between
each element.

irb(main):003:0> _.join("-")
=> “Benefits-And-Features”

Sam D. 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.

hth,

Siep

Very nice =]

Ralph S. wrote in post #967293:

Assume I have camelized string like
BenefitsAndFeatures
and I want to convert that to
Benefits-And-Features
What is a good “Ruby way” to do that?

Second version:

“BenefitsAndFeatures”.split(/(?=[A-Z])/).join(’-’)

On Dec 8, 5:47pm, Ralph S. [email protected] wrote:

[Note: parts of this message were removed to make it a legal post.]

Assume I have camelized string like
BenefitsAndFeatures
and I want to convert that to
Benefits-And-Features

What is a good “Ruby way” to do that?

The good Ruby way:

“BenefitsAndFeatures”.gsub( /.(?=[[:upper:]])/, ‘&-’ )
==>“Benefits-And-Features”

On Thu, Dec 9, 2010 at 8:02 AM, Ralph S. [email protected] wrote:

This is sooo far beyond my skill level.

maybe try simple first, ie, get lower and up chars and put a dash in bw.

eg

“BenefitsAndFeatures”.gsub /([a-z])([A-Z])/ , ‘\1-\2’

best regards -botp

On 09.12.2010 02:18, Sam D. wrote:

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

On Thu, Dec 9, 2010 at 9:14 AM, botp [email protected] wrote:

On Thu, Dec 9, 2010 at 8:02 AM, Ralph S. [email protected] wrote:

This is sooo far beyond my skill level.

maybe try simple first, ie, get lower and up chars and put a dash in bw.

eg

“BenefitsAndFeatures”.gsub /([a-z])([A-Z])/ , ‘\1-\2’

Much better! Slap hand on face.

Cheers

robert