Extracting vowels and consonants using regular expression

Discussion in 'Ruby' started by Dondi, Feb 2, 2008.

  1. Dondi

    Dondi Guest

    I am trying to parse a string and extract all vowels and consonants
    into two separate substrings. However, I can't get my solution to
    work. Any pointers are appreciated. Here is the approach I am using:

    1) Extended the String Class with the following methods:

    class String
    def vowels
    self.scan(/[aeiou]|(?![aeiou])y(?![aeiou])/i)
    end

    def consonants
    self.scan(/![aeiou]|(?=[aeiou])y(?=[aeiou])/i)
    end
    end

    2) Invoke the methods:

    test_paragraph = "Mary had a little lamb"
    @vowel_sub_str = test_paragraph.vowels
    @consonant_sub_str = test_paragraph.consonants

    However, the result is just two empty strings. I believe the problem
    is in the regular expression, but I can't figure out just where. Any
    ideas/pointers are appreciated.

    Thanks.
     
    Dondi, Feb 2, 2008
    #1
    1. Advertising

  2. Alle Saturday 02 February 2008, Dondi ha scritto:
    > I am trying to parse a string and extract all vowels and consonants
    > into two separate substrings. However, I can't get my solution to
    > work. Any pointers are appreciated. Here is the approach I am using:
    >
    > 1) Extended the String Class with the following methods:
    >
    > class String
    > def vowels
    > self.scan(/[aeiou]|(?![aeiou])y(?![aeiou])/i)
    > end
    >
    > def consonants
    > self.scan(/![aeiou]|(?=[aeiou])y(?=[aeiou])/i)
    > end
    > end
    >
    > 2) Invoke the methods:
    >
    > test_paragraph = "Mary had a little lamb"
    > @vowel_sub_str = test_paragraph.vowels
    > @consonant_sub_str = test_paragraph.consonants
    >
    > However, the result is just two empty strings. I believe the problem
    > is in the regular expression, but I can't figure out just where. Any
    > ideas/pointers are appreciated.
    >
    > Thanks.


    A simple (but I fear not too efficient) way is:

    class String

    def vowels
    gsub(/[^aeiou]/, '')
    end

    def consonants
    gsub(/[aeiou]/, '')
    end

    end

    Stefano
     
    Stefano Crocco, Feb 2, 2008
    #2
    1. Advertising

  3. Dondi

    Robert Dober Guest

    >
    > A simple (but I fear not too efficient) way is:

    Not sure if OP excluded non letters from the input
    >
    > class String
    >
    > def vowels
    > gsub(/[^aeiou]/, '')
    > end
    >
    > def consonants
    > gsub(/[aeiou]/, '')
    > end
    >
    > end
    >
    > Stefano
    >
    >
    >

    This shall do it in one run, quite complicated code, interested if
    something more elegant can be found.
    class String
    def v_c
    each_char.inject(["",""]){ |r, c|
    case c
    when /[aeiouy]/i
    [r.first << c, r.last ]
    when /[^a-z]/i
    r
    else
    [r.first, r.last << c ]
    end
    }
    end
    end

    HTH
    Robert

    --
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
     
    Robert Dober, Feb 2, 2008
    #3
  4. Dondi

    Corey Haines Guest

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

    Elegant? I don't know, but I kind of like it. :)

    "corey".split(//).partition { |x| "aeiouAEIOU"[x] }

    returns

    => [["o", "e"], ["c", "r", "y"]]

    irb(main):006:0> "corey haines".split(//).partition { |x| "aeiouAEIOU"[x] }
    => [["o", "e", "a", "i", "e"], ["c", "r", "y", " ", "h", "n", "s"]]

    you might want to skip spaces


    -Corey



    On Feb 2, 2008 12:38 PM, Robert Dober <> wrote:

    > >
    > > A simple (but I fear not too efficient) way is:

    > Not sure if OP excluded non letters from the input
    > >
    > > class String
    > >
    > > def vowels
    > > gsub(/[^aeiou]/, '')
    > > end
    > >
    > > def consonants
    > > gsub(/[aeiou]/, '')
    > > end
    > >
    > > end
    > >
    > > Stefano
    > >
    > >
    > >

    > This shall do it in one run, quite complicated code, interested if
    > something more elegant can be found.
    > class String
    > def v_c
    > each_char.inject(["",""]){ |r, c|
    > case c
    > when /[aeiouy]/i
    > [r.first << c, r.last ]
    > when /[^a-z]/i
    > r
    > else
    > [r.first, r.last << c ]
    > end
    > }
    > end
    > end
    >
    > HTH
    > Robert
    >
    > --
    > http://ruby-smalltalk.blogspot.com/
    >
    > ---
    > Whereof one cannot speak, thereof one must be silent.
    > Ludwig Wittgenstein
    >
    >



    --
    http://www.coreyhaines.com
    The Internet's Premiere source of information about Corey Haines
     
    Corey Haines, Feb 2, 2008
    #4
  5. Dondi

    Robert Dober Guest

    On Feb 2, 2008 7:27 PM, Corey Haines <> wrote:
    > Elegant? I don't know, but I kind of like it. :)
    >
    > "corey".split(//).partition { |x| "aeiouAEIOU"[x] }
    >
    > returns
    >
    > => [["o", "e"], ["c", "r", "y"]]
    >
    > irb(main):006:0> "corey haines".split(//).partition { |x| "aeiouAEIOU"[x] }
    > => [["o", "e", "a", "i", "e"], ["c", "r", "y", " ", "h", "n", "s"]]
    >
    > you might want to skip spaces
    >
    >
    > -Corey

    Excellent Corey, allow me to make it fit *any* string, I still have
    not seen any statement that we are treating letters only ;)

    "cor4ey ha2ines".scan(/[a-z]/i).join.each_char.partition { |x| "aeiouAEIOU"[x] }

    Robert
    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
     
    Robert Dober, Feb 2, 2008
    #5
  6. Dondi

    Robert Dober Guest

    >
    > "cor4ey ha2ines".scan(/[a-z]/i).join.each_char.partition { |x| "aeiouAEIOU"[x] }

    and some golfing

    { |x| /[aeiou]/i =~ x }

    R.
     
    Robert Dober, Feb 2, 2008
    #6
  7. Dondi

    Corey Haines Guest

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

    Sweet! I like pulling the numbers out, but I get this in irb

    irb(main):001:0> "cor4ey ha2ines".scan(/[a-z]/i).join.each_char.partition {
    |x| "aeiouAEIOU"[x] }
    NoMethodError: undefined method `each_char' for "coreyhaines":String

    Why not use split(//) instead of each_char?


    On Feb 2, 2008 5:04 PM, Robert Dober <> wrote:

    > On Feb 2, 2008 7:27 PM, Corey Haines <> wrote:
    > > Elegant? I don't know, but I kind of like it. :)
    > >
    > > "corey".split(//).partition { |x| "aeiouAEIOU"[x] }
    > >
    > > returns
    > >
    > > => [["o", "e"], ["c", "r", "y"]]
    > >
    > > irb(main):006:0> "corey haines".split(//).partition { |x|

    > "aeiouAEIOU"[x] }
    > > => [["o", "e", "a", "i", "e"], ["c", "r", "y", " ", "h", "n", "s"]]
    > >
    > > you might want to skip spaces
    > >
    > >
    > > -Corey

    > Excellent Corey, allow me to make it fit *any* string, I still have
    > not seen any statement that we are treating letters only ;)
    >
    > "cor4ey ha2ines".scan(/[a-z]/i).join.each_char.partition { |x|
    > "aeiouAEIOU"[x] }
    >
    > Robert
    > ---
    > Whereof one cannot speak, thereof one must be silent.
    > Ludwig Wittgenstein
    >
    >



    --
    http://www.coreyhaines.com
    The Internet's Premiere source of information about Corey Haines
     
    Corey Haines, Feb 2, 2008
    #7
  8. Dondi

    Corey Haines Guest

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

    :) Cool. I wasn't sure exactly how to do that. Now I do! And, in the words
    of one of my personal favorite philosophers, "Knowing is half the battle."

    On Feb 2, 2008 5:07 PM, Robert Dober <> wrote:

    > >
    > > "cor4ey ha2ines".scan(/[a-z]/i).join.each_char.partition { |x|

    > "aeiouAEIOU"[x] }
    > and some golfing
    >
    > { |x| /[aeiou]/i =~ x }
    >
    > R.
    >
    >



    --
    http://www.coreyhaines.com
    The Internet's Premiere source of information about Corey Haines
     
    Corey Haines, Feb 2, 2008
    #8
  9. Dondi

    Robert Dober Guest

    Oh sorry using 1.9 excluseviley now...
    Robert
     
    Robert Dober, Feb 2, 2008
    #9
  10. Dondi

    Corey Haines Guest

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

    Okay. Sounds good. I'm still on 1.8.

    Thanks for the ideas.

    -Corey

    On Feb 2, 2008 5:17 PM, Robert Dober <> wrote:

    > Oh sorry using 1.9 excluseviley now...
    > Robert
    >
    >



    --
    http://www.coreyhaines.com
    The Internet's Premiere source of information about Corey Haines
     
    Corey Haines, Feb 2, 2008
    #10
    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. VSK
    Replies:
    2
    Views:
    2,354
  2. Phil Slater
    Replies:
    8
    Views:
    1,111
    Howard
    May 17, 2004
  3. Matt
    Replies:
    7
    Views:
    1,649
    Oliver Wong
    Jan 2, 2007
  4. ivalki
    Replies:
    5
    Views:
    1,082
    Chris ( Val )
    Jan 16, 2007
  5. nwaits
    Replies:
    10
    Views:
    237
Loading...

Share This Page