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
···
--
Posted via http://www.ruby-forum.com/.
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
--
Posted via http://www.ruby-forum.com/.
i wanting to write a regular expression which matches words which
contain the vowels [aeiou] in alphabetical orderim not sure where to start so any help would be greatly appreciated
This will get you started:
IRB:
>> re = /[^aeiou]*[a][^aeiou]*[e][^aeiou]*[i][^aeiou]*[o][^aeiou]*[u][^aeiou]*/
=> [aeiou][a][aeiou][e][aeiou][i][aeiou][o][aeiou][u][aeiou]
>> 'abstemious' =~ re
=> 0
>> 'facetious' =~ re
=> 0
>> 'bstemious' =~ re
=> nil
>> 'ebstamious' =~ re
=> 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
On 16 Jan 2008, at 15:57, Johnathan Smith wrote:
-------
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
On 1/16/08, Johnathan Smith <stu_09@hotmail.com> wrote:
hi there
i wanting to write a regular expression which matches words which
contain the vowels [aeiou] in alphabetical orderim not sure where to start so any help would be greatly appreciated
thans
--
Posted via http://www.ruby-forum.com/\.
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
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.
On Jan 16, 2008 4:57 PM, Johnathan Smith <stu_09@hotmail.com> wrote:
hi there
i wanting to write a regular expression which matches words which
contain the vowels [aeiou] in alphabetical orderim not sure where to start so any help would be greatly appreciated
>> re = /[^aeiou]*[a][^aeiou]*[e][^aeiou]*[i][^aeiou]*[o][^aeiou]*[u][^aeiou]*/
Oops, that should have been:
>> re = /[^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u[^aeiou]*/
Regards,
Andy Stewart
On 16 Jan 2008, at 16:52, Andrew Stewart wrote:
-------
Rick Denatale wrote:
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
--
Posted via http://www.ruby-forum.com/\.
Andrew Stewart wrote:
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
--
Posted via http://www.ruby-forum.com/\.
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.
On 1/16/08, Wolfgang Nádasi-Donner <ed.odanow@wonado.de> wrote:
Rick Denatale wrote:
> Something like this?
> irb(main):001:0> "frabelious" =~ /a.*?e.*?i.*?o.*?u/
> => 2This 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".
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
try something like:
/a+[^a]*e+[^ae]*i+[^aei]*o+[^aeio]*u+/
On Jan 16, 2008 8:03 PM, Wolfgang Nádasi-Donner <ed.odanow@wonado.de> wrote:
Rick Denatale wrote:
> Something like this?
> irb(main):001:0> "frabelious" =~ /a.*?e.*?i.*?o.*?u/
> => 2This 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
--Posted via http://www.ruby-forum.com/\.
Yikes!
That's clever though my brain copes better with this halfway house:
consonant = /[^aeiou]/
re = /#{consonant}*a#{consonant}*e#{consonant}*i#{consonant}*o#{consonant}*u#{consonant}*/
Or, perhaps, in-between the in-betweens:
vowels = %w( a e i o u )
consonant = '[^aeiou]'
re = /#{vowels.push('').unshift('').join("#{consonant}*")}/
Regards,
Andy Stewart
On 16 Jan 2008, at 22:19, Wolfgang Nádasi-Donner wrote:
Andrew Stewart wrote:
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]*$/
-------
Rick Denatale wrote:
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
--
Posted via http://www.ruby-forum.com/\.