Word starts by a vowel

G

Greg Ma

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...
 
A

Ammar Ali

2010/10/28 Jes=C3=BAs Gabriel y Gal=C3=A1n said:
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" =3D~ /\A[aeiou]/
=3D> 0
irb(main):002:0> "vfvdasdfsifnweif" =3D~ /\A[aeiou]/
=3D> nil
irb(main):005:0> "asdfsifnweif"[0,1] =3D~ /[aeiou]/
=3D> 0
irb(main):006:0> "vfvdasdfsifnweif"[0,1] =3D~ /[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" =3D~ /^[aeiou]/i =3D> 0
"apple" =3D~ /^[aeiou]/i
=3D> 0

Regards,
Ammar
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top