regular expression

J

Johnathan Smith

hi there

i wanting to write a regular expression which matches words which
contain the vowels [aeiou] in alphabetical order

im not sure where to start so any help would be greatly appreciated

thans
 
A

Andrew Stewart

i wanting to write a regular expression which matches words which
contain the vowels [aeiou] in alphabetical order

im not sure where to start so any help would be greatly appreciated

This will get you started:

IRB:
re = /[^aeiou]*[a][^aeiou]*[e][^aeiou]*[^aeiou]*[o][^aeiou]*

[^aeiou]*/
=> [aeiou][a][aeiou][e][aeiou][aeiou][o][aeiou][aeiou]=> nil

You can see there's a lot of repetition in the regular expression so
the next step would probably be to DRY it. I leave that as an
exercise to the reader (because I can't work out how to do it :)

You'll may also wish to anchor the regular expression so it doesn't
cross words.

Regards,
Andy Stewart
 
R

Rick DeNatale

hi there

i wanting to write a regular expression which matches words which
contain the vowels [aeiou] in alphabetical order

im not sure where to start so any help would be greatly appreciated

thans

Something like this?
irb(main):001:0> "frabelious" =~ /a.*?e.*?i.*?o.*?u/
=> 2
irb(main):002:0> "frabeelious" =~ /a.*?e.*?i.*?o.*?u/
=> 2
irb(main):003:0> "friaeelaous" =~ /a.*?e.*?i.*?o.*?u/
=> nil
 
W

Wolfgang Nádasi-Donner

Rick said:
Something like this?
irb(main):001:0> "frabelious" =~ /a.*?e.*?i.*?o.*?u/
=> 2

This doesn't work as expected...

irb(main):001:0> "xaxxixxexxixxuxxoxxuxx".match(/a.*?e.*?i.*?o.*?u/)[0]
=> "axxixxexxixxuxxoxxu"

..., because it allows all wovels in any order in the word, if there is
one possible grouping with the wanted ordering - The subpattern "a.*?e"
for example allows any number of "a"s, "i"s, "o"s, and "u"s between the
"a" and the "e".

Wolfgang Nádasi-Donner
 
W

Wolfgang Nádasi-Donner

Andrew said:
You can see there's a lot of repetition in the regular expression so
the next step would probably be to DRY it.

O.K. - it really increases the readability ;-)

re = Regexp.compile((v=p='')+"^#{p="[^#{v='aeiou'}]*"}"+v.split('').
join("[^#{v}]*")+p+'$')
p re # =>
/^[^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u[^aeiou]*$/

Wolfgang Nádasi-Donner
 
R

Rick DeNatale

Rick said:
Something like this?
irb(main):001:0> "frabelious" =3D~ /a.*?e.*?i.*?o.*?u/
=3D> 2

This doesn't work as expected...

irb(main):001:0> "xaxxixxexxixxuxxoxxuxx".match(/a.*?e.*?i.*?o.*?u/)[0]
=3D> "axxixxexxixxuxxoxxu"

..., because it allows all wovels in any order in the word, if there is
one possible grouping with the wanted ordering - The subpattern "a.*?e"
for example allows any number of "a"s, "i"s, "o"s, and "u"s between the
"a" and the "e".

Ahh, but "xaxxixxexxixxuxxoxxuxx" DOES contain the vowels in
alphabetical order, the original problem statement said nothing about
disallowing additional vowels.

I really posted my "solution" to drive out what he really wanted.
Kind of a test-driven approach.




--=20
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
 
M

Moises Trovo

Rick said:
Something like this?
irb(main):001:0> "frabelious" =3D~ /a.*?e.*?i.*?o.*?u/
=3D> 2

This doesn't work as expected...

irb(main):001:0> "xaxxixxexxixxuxxoxxuxx".match(/a.*?e.*?i.*?o.*?u/)[0]
=3D> "axxixxexxixxuxxoxxu"

..., because it allows all wovels in any order in the word, if there is
one possible grouping with the wanted ordering - The subpattern "a.*?e"
for example allows any number of "a"s, "i"s, "o"s, and "u"s between the
"a" and the "e".

Wolfgang N=E1dasi-Donner

try something like:
/a+[^a]*e+[^ae]*i+[^aei]*o+[^aeio]*u+/
 
W

Wolfgang Nádasi-Donner

Rick said:
I really posted my "solution" to drive out what he really wanted.
Kind of a test-driven approach.

Oh - I see. Let's wait for the more complete specification ;-)

Wolfgang Nádasi-Donner
 
A

Andrew Stewart

Andrew said:
You can see there's a lot of repetition in the regular expression so
the next step would probably be to DRY it.

O.K. - it really increases the readability ;-)

re =3D Regexp.compile((v=3Dp=3D'')+"^#{p=3D"[^#{v=3D'aeiou'}]*"}"+v.spli= t('').
join("[^#{v}]*")+p+'$')
p re # =3D>
/^[^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u[^aeiou]*$/

Yikes!

That's clever though my brain copes better with this halfway house:

consonant =3D /[^aeiou]/
re =3D /#{consonant}*a#{consonant}*e#{consonant}*i#{consonant}*o#=20
{consonant}*u#{consonant}*/

Or, perhaps, in-between the in-betweens:

vowels =3D %w( a e i o u )
consonant =3D '[^aeiou]'
re =3D /#{vowels.push('').unshift('').join("#{consonant}*")}/

Regards,
Andy Stewart
 
E

Eivind Eklund

hi there

i wanting to write a regular expression which matches words which
contain the vowels [aeiou] in alphabetical order

im not sure where to start so any help would be greatly appreciated

Start with regular expression documentation; I think that's what your
teacher would want, instead of just having ruby-talk do your homework
for you.

http://ysomeya.hp.infoseek.co.jp/eng-quick_regex.html is one place to
start (very short); http://evolt.org/article/thelist/20/22700/ is
another, more extensive tutorial.

Neither of these are Ruby specific - this is OK, as what you need is
to understand the language regular expressions, not the language Ruby.

Eivind.
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top