Word starts by a vowel

Hi,
How could I know if a word starts by a vowel?
I couln't find it online and I am a real nooby with regex...

···

--
Posted via http://www.ruby-forum.com/.

For this kind of task, you use a regex with a character class that
contains the valid characters you want to check.
You also need to anchor it at the start of the string to match only
that position (or try to match only the first character):

irb(main):001:0> "asdfsifnweif" =~ /\A[aeiou]/
=> 0
irb(main):002:0> "vfvdasdfsifnweif" =~ /\A[aeiou]/
=> nil
irb(main):005:0> "asdfsifnweif"[0,1] =~ /[aeiou]/
=> 0
irb(main):006:0> "vfvdasdfsifnweif"[0,1] =~ /[aeiou]/

Jesus.

···

On Thu, Oct 28, 2010 at 10:49 AM, Greg Ma <gregorylepacha@msn.com> wrote:

Hi,
How could I know if a word starts by a vowel?
I couln't find it online and I am a real nooby with regex...

Hi,
How could I know if a word starts by a vowel?
I couln't find it online and I am a real nooby with regex...

For this kind of task, you use a regex with a character class that
contains the valid characters you want to check.
You also need to anchor it at the start of the string to match only
that position (or try to match only the first character):

irb(main):001:0> "asdfsifnweif" =~ /\A[aeiou]/
=> 0
irb(main):002:0> "vfvdasdfsifnweif" =~ /\A[aeiou]/
=> nil
irb(main):005:0> "asdfsifnweif"[0,1] =~ /[aeiou]/
=> 0
irb(main):006:0> "vfvdasdfsifnweif"[0,1] =~ /[aeiou]/

Jesus.

I would like to add to Jesus' good advice that you can add a case
insensitive option (the letter i at the end) to match lower and upper
case vowels:

"Apple" =~ /^[aeiou]/i

=> 0

"apple" =~ /^[aeiou]/i

=> 0

Regards,
Ammar

···

2010/10/28 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:

On Thu, Oct 28, 2010 at 10:49 AM, Greg Ma <gregorylepacha@msn.com> wrote: