[oak perl] Regular Expressions

Belden Lyman blyman at iii.com
Thu Mar 11 17:32:15 CST 2004


On Thu, 2004-03-11 at 09:25, Tony Stubblebine wrote:
> Belden,
> 
> I like how you're using lookahead (and comments and whitespace).
> 
> Your third lookahead is checking to make sure there's at least one 
> vowel. The alternation is fine for two character words, but doesn't work 
> for longer words like "strength," where the first vowel is in position four.
> 

This surprises me, because this works:

   $ echo 'strength' | perl -ne '/(?:.[aeiouy]|[aeiouy].)/
     and print'

but this fails:

   $ echo 'strength' | perl -ne '/(?=^\w+$)(?:.[aeiouy]|[aeiouy].)/
     and print'

It appears that adding "(?=^\w+$)" is affecting my subsequent 
"(?:)" so the whole regex acts as

   /^.[aeiouy]|[aeiouy].$/

Obviously it's the addition of the lookahead that's making things
go pear shaped. Reading and re-reading perlre.pod's description of 
"(?=pattern)" isn't helping me understand this change in behavior.

Can anyone explain this?

Belden




More information about the Oakland mailing list