Splitting on capital letters

R

Ralph Shnelvar

[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?
 
J

Jeremy Bopp

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"
 
R

Ralph Shnelvar

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

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
 
T

Tony Arcieri

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

Here's an ActiveSupport-powered roflscale method:
"BenefitsAndFeatures".underscore.split('_').map(&:capitalize).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
 
S

Sam Duncan

This might be ridiculous, but ...

("BenefitsAndFeatures".gsub(/[A-Z]/) { |c| "-#{c}"
}).reverse.chomp('-').reverse

Sam


Here's an ActiveSupport-powered roflscale method:

"BenefitsAndFeatures".underscore.split('_').map(&:capitalize).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
 
K

Kristofer M White

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
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"


--
 
S

Siep Korteling

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.

hth,

Siep
 
S

Siep Korteling

Ralph Shnelvar 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('-')
 
S

Sam Duncan

Very nice =]

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.

hth,

Siep
 
R

Robert Klemme

Very nice =]

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
 
W

w_a_x_man

[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"
 
B

botp

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top