[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

regular expression

Johnathan Smith

1/16/2008 3:58:00 PM

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

10 Answers

Andrew Stewart

1/16/2008 4:52:00 PM

0


On 16 Jan 2008, at 15:57, Johnathan Smith 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 :)

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

Regards,
Andy Stewart

-------
http://airbladeso...




Rick DeNatale

1/16/2008 4:53:00 PM

0

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 order
>
> im not sure where to start so any help would be greatly appreciated
>
> thans
> --
> Posted via http://www.ruby-....
>
>

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

Andrew Stewart

1/16/2008 5:14:00 PM

0


On 16 Jan 2008, at 16:52, Andrew Stewart 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 Stewart

-------
http://airbladeso...




Wolfgang Nádasi-donner

1/16/2008 10:03:00 PM

0

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

Wolfgang Nádasi-donner

1/16/2008 10:19:00 PM

0

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

Rick DeNatale

1/16/2008 10:48:00 PM

0

On 1/16/08, Wolfgang N=E1dasi-Donner <ed.odanow@wonado.de> wrote:
> Rick Denatale wrote:
> > 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.denh...

Moises Trovo

1/16/2008 11:16:00 PM

0

On Jan 16, 2008 8:03 PM, Wolfgang N=E1dasi-Donner <ed.odanow@wonado.de> wro=
te:
> Rick Denatale wrote:
> > 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
> --
>
> Posted via http://www.ruby-....
>
>

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

Wolfgang Nádasi-donner

1/17/2008 12:44:00 AM

0

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

Andrew Stewart

1/17/2008 11:52:00 AM

0


On 16 Jan 2008, at 22:19, Wolfgang N=E1dasi-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 =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

-------
http://airbladeso...




Eivind Eklund

1/17/2008 12:55:00 PM

0

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 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_... is one place to
start (very short); http://evolt.org/article/thelist... 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.