From: Andrew Stewart Date: 2008-01-17T20:51:37+09:00 Subject: Re: regular expression 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]*$/ 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 ------- http://airbladesoftware.com