Splitting on capital letters

Discussion in 'Ruby' started by Ralph Shnelvar, Dec 8, 2010.

  1. [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?
    Ralph Shnelvar, Dec 8, 2010
    #1
    1. Advertising

  2. Ralph Shnelvar

    Jeremy Bopp Guest

    On 12/08/2010 05:47 PM, Ralph Shnelvar 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"
    Jeremy Bopp, Dec 8, 2010
    #2
    1. Advertising

  3. [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
    Ralph Shnelvar, Dec 9, 2010
    #3
  4. Ralph Shnelvar

    Tony Arcieri Guest

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


    On Wed, Dec 8, 2010 at 5:02 PM, Ralph Shnelvar <> wrote:

    > 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





    --
    Tony Arcieri
    Medioh! Kudelski
    Tony Arcieri, Dec 9, 2010
    #4
  5. Ralph Shnelvar

    Sam Duncan Guest

    This might be ridiculous, but ...

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

    Sam


    On 09/12/10 13:04, Tony Arcieri wrote:
    > Here's an ActiveSupport-powered roflscale method:
    >
    >
    >> "BenefitsAndFeatures".underscore.split('_').map(&:capitalize).join('-')
    >>

    > => "Benefits-And-Features"
    >
    >
    > On Wed, Dec 8, 2010 at 5:02 PM, Ralph Shnelvar<> wrote:
    >
    >
    >> 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
    >>

    >
    >
    >
    >
    Sam Duncan, Dec 9, 2010
    #5
  6. On 12/08/2010 06:02 PM, Ralph Shnelvar wrote:
    > 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"


    --
    ---
    Thanks,
    Kristofer M White
    http://www.kmwhite.net
    https://www.github.com/kmwhite/
    Kristofer M White, Dec 9, 2010
    #6
  7. 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

    --
    Posted via http://www.ruby-forum.com/.
    Siep Korteling, Dec 9, 2010
    #7
  8. 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('-')

    --
    Posted via http://www.ruby-forum.com/.
    Siep Korteling, Dec 9, 2010
    #8
  9. Ralph Shnelvar

    Sam Duncan Guest

    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.
    >
    > hth,
    >
    > Siep
    >
    >
    Sam Duncan, Dec 9, 2010
    #9
  10. 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/
    Robert Klemme, Dec 9, 2010
    #10
  11. Ralph Shnelvar

    w_a_x_man Guest

    On Dec 8, 5:47 pm, Ralph Shnelvar <> 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"
    w_a_x_man, Dec 9, 2010
    #11
  12. Ralph Shnelvar

    botp Guest

    On Thu, Dec 9, 2010 at 8:02 AM, Ralph Shnelvar <> 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
    botp, Dec 9, 2010
    #12
  13. On Thu, Dec 9, 2010 at 9:14 AM, botp <> wrote:
    > On Thu, Dec 9, 2010 at 8:02 AM, Ralph Shnelvar <> 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
    >
    > =A0"BenefitsAndFeatures".gsub /([a-z])([A-Z])/ , '\1-\2'


    Much better! Slap hand on face.

    Cheers

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Dec 9, 2010
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ebi
    Replies:
    2
    Views:
    12,597
    puzzlecracker
    Feb 5, 2005
  2. Eric Wertman
    Replies:
    2
    Views:
    320
  3. Miguel Orrego

    Capital Letters

    Miguel Orrego, Aug 19, 2003, in forum: ASP General
    Replies:
    6
    Views:
    227
    Dave Anderson
    Aug 20, 2003
  4. Keith Bowes

    Avoid All Capital Letters in Guestbook

    Keith Bowes, Jan 6, 2004, in forum: Javascript
    Replies:
    4
    Views:
    782
    Thomas 'PointedEars' Lahn
    Jan 15, 2004
  5. Venugopal
    Replies:
    11
    Views:
    1,497
    Tassilo v. Parseval
    Nov 5, 2003
Loading...

Share This Page