in reply to Negative Lookahead Assertion Strangness

In example 3, the lookahead assertion doesn't match at the beginning of the string, so the regex tries to match at the next position in the string, that fails and so on. This continues until the regex matches at position "oo then..." in which case no foo is found and it succeeds.

In example 4, the .* will first eat the whole string and then the foo will fail to match, satisfying the negative assertion :). So the whole string is printed.

Both of these examples show the danger of creating nonlocal assertions. It is easy to do something unexpected when the string tested is some distance from the assertion.


  • Comment on Re: Negative Lookahead Assertion Strangness