in reply to Negative Lookahead Assertion Strangness

Three says /(?!.*foo)(.*bar.*)/, which means "does there exist the leftmost place in the string where I could look ahead and not match a foo, and yet I can look ahead and match a bar (which will be captured)?" And yes, as soon as we've gone past that "f", it qualifies.

Four says /(?!^.*foo)(.*bar.*)/), which asks "does there exist the leftmost place in the string where we can look ahead and not see the begining of the string (followed by other stuff), and then some "bar" later (which is captured)?" And yes, as soon as we leave the beginning of the string, we can no longer "look forward to see the beginning of the string". So it matches right after the first char.

If you are careful about how you read the regex, it'll become clear what it's trying to match and capture. You just have to be careful. :)

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^2: Negative Lookahead Assertion Strangness
by Fletch (Bishop) on May 15, 2006 at 17:32 UTC

    YAPE::Regex::Explain verbosely telling you what a regex is saying may help if you still can't see why something's matching (or not).