[oak perl] Regular Expressions
Tony Stubblebine
tonys at oreillynet.com
Thu Mar 11 17:37:35 CST 2004
Belden--
The lookahead is a positional construct. It has to match at the position
right before your alternation. Since there's another positional
construct in your lookahead (^), you've effectively anchored the entire
regex to the start of the string. In this case there isn't a vowel in
the first two letters of "strength."
I suppose you could put a \w* between the lookahead and the alternation
clause. That would allow the lookahead to anchor at the start of the
string and your alternation to float across the rest of the string.
--Tony
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?
>
>Belden
>
>_______________________________________________
>Oakland mailing list
>Oakland at mail.pm.org
>http://mail.pm.org/mailman/listinfo/oakland
>
>
More information about the Oakland
mailing list