# Regular expression

**URL:** <https://rubytalk.org/t/regular-expression/43822>\
**Category:** ruby-talk\
**Created:** [16 January 2008 15:57 UTC](https://rubytalk.org/t/regular-expression/43822 "2008-01-16T15:57:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johnathan\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Johnathan\_Smith](https://rubytalk.org/u/Johnathan_Smith)\
**Post date:** [16 January 2008 15:57 UTC](https://rubytalk.org/t/regular-expression/43822/1 "2008-01-16T15:57:59Z")

</div>

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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Andrew\_Stewart](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Andrew\_Stewart](https://rubytalk.org/u/Andrew_Stewart)\
**Post date:** [16 January 2008 16:52 UTC](https://rubytalk.org/t/regular-expression/43822/2 "2008-01-16T16:52:29Z")

</div>

> 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

> **···**
>
> On 16 Jan 2008, at 15:57, Johnathan Smith wrote:
> 
> -------
> 
> > **[AirBlade Software](https://airbladesoftware.com/)**
> >
> > Andrew Stewart of AirBlade Software.

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [16 January 2008 16:53 UTC](https://rubytalk.org/t/regular-expression/43822/3 "2008-01-16T16:53:12Z")

</div>

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 order
> > 
> > im not sure where to start so any help would be greatly appreciated
> > 
> > thans  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Eivind\_Eklund](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Eivind\_Eklund](https://rubytalk.org/u/Eivind_Eklund)\
**Post date:** [17 January 2008 12:55 UTC](https://rubytalk.org/t/regular-expression/43822/4 "2008-01-17T12:55:03Z")

</div>

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](http://ysomeya.hp.infoseek.co.jp/eng-quick_regex.html) is one place to  
start (very short); [http://evolt.org/article/thelist/20/22700/](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 order
> > 
> > im not sure where to start so any help would be greatly appreciated

---

<div class="post-metadata">

**Author:** ![Andrew\_Stewart](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Andrew\_Stewart](https://rubytalk.org/u/Andrew_Stewart)\
**Post date:** [16 January 2008 17:13 UTC](https://rubytalk.org/t/regular-expression/43822/5 "2008-01-16T17:13:32Z")

</div>

> \>\> 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:
> 
> -------
> 
> > **[AirBlade Software](https://airbladesoftware.com/)**
> >
> > Andrew Stewart of AirBlade Software.

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Nadasi-Donn](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@Wolfgang\_Nadasi-Donn](https://rubytalk.org/u/Wolfgang_Nadasi-Donn)\
**Post date:** [16 January 2008 22:03 UTC](https://rubytalk.org/t/regular-expression/43822/6 "2008-01-16T22:03:16Z")

</div>

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Nadasi-Donn](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@Wolfgang\_Nadasi-Donn](https://rubytalk.org/u/Wolfgang_Nadasi-Donn)\
**Post date:** [16 January 2008 22:19 UTC](https://rubytalk.org/t/regular-expression/43822/7 "2008-01-16T22:19:25Z")

</div>

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('').  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [16 January 2008 22:47 UTC](https://rubytalk.org/t/regular-expression/43822/8 "2008-01-16T22:47:57Z")

</div>

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/  
> > \> =\> 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".
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Moises\_Trovo](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Moises\_Trovo](https://rubytalk.org/u/Moises_Trovo)\
**Post date:** [16 January 2008 23:15 UTC](https://rubytalk.org/t/regular-expression/43822/9 "2008-01-16T23:15:47Z")

</div>

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/  
> > \> =\> 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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Andrew\_Stewart](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Andrew\_Stewart](https://rubytalk.org/u/Andrew_Stewart)\
**Post date:** [17 January 2008 11:51 UTC](https://rubytalk.org/t/regular-expression/43822/10 "2008-01-17T11:51:37Z")

</div>

Yikes!

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

&nbsp;&nbsp;&nbsp;consonant = /[^aeiou]/  
&nbsp;&nbsp;&nbsp;re = /#{consonant}\*a#{consonant}\*e#{consonant}\*i#{consonant}\*o#{consonant}\*u#{consonant}\*/

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

&nbsp;&nbsp;&nbsp;vowels = %w( a e i o u )  
&nbsp;&nbsp;&nbsp;consonant = '[^aeiou]'  
&nbsp;&nbsp;&nbsp;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('').  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join("[^#{v}]\*")+p+'$')  
> > p re # =\>  
> > /^[^aeiou]\*a[^aeiou]\*e[^aeiou]\*i[^aeiou]\*o[^aeiou]\*u[^aeiou]\*$/
> 
> -------
> 
> > **[AirBlade Software](https://airbladesoftware.com/)**
> >
> > Andrew Stewart of AirBlade Software.

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Nadasi-Donn](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@Wolfgang\_Nadasi-Donn](https://rubytalk.org/u/Wolfgang_Nadasi-Donn)\
**Post date:** [17 January 2008 00:43 UTC](https://rubytalk.org/t/regular-expression/43822/11 "2008-01-17T00:43:58Z")

</div>

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/\](http://www.ruby-forum.com/%5C).
