[oak perl] Regular Expressions

David Fetter david at fetter.org
Thu Mar 11 17:37:33 CST 2004


On Thu, Mar 11, 2004 at 03:26:14PM -0800, Belden Lyman wrote:
> 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?

Um, yeah.  You're writing code as cleverly as possible, and here's
what Kernighan had to say about that:

    Debugging is twice as hard as writing the code in the first
    place.  Therefore, if you write the code as cleverly as possible,
    you are, by definition, not smart enough to debug it.

Belden, you're a smart, sensible guy.  Why are you doing this to
yourself?

Cheers,
D
-- 
David Fetter david at fetter.org http://fetter.org/
phone: +1 510 893 6100   mobile: +1 415 235 3778

Remember to vote!



More information about the Oakland mailing list