Regular expression

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

On 16 Jan 2008, at 15:57, Johnathan S. wrote:

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][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 :slight_smile:

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

Regards,
Andy S.

On 1/16/08, Johnathan S. [email protected] 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


Rick DeNatale

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

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

On 16 Jan 2008, at 16:52, Andrew S. wrote:

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

Andrew S. 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 :wink:

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

On 1/16/08, Wolfgang Nádasi-Donner [email protected] wrote:

…, 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.


Rick DeNatale

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

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 :wink:

Wolfgang Nádasi-Donner

On 16 Jan 2008, at 22:19, Wolfgang Nádasi-Donner wrote:

Andrew S. 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 :wink:

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]$/

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

On Jan 16, 2008 8:03 PM, Wolfgang Nádasi-Donner [email protected]
wrote:

…, 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/.

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

On Jan 16, 2008 4:57 PM, Johnathan S. [email protected] wrote:

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs