Re: regexp lookahead



Michael Powe writes:

I experimented a bit with the Java regexp lookahead functionality,
and the results don't make sense to me. The test is below.

....

Negative lookahead:

String re = "(.*)\\[(?!\\S+)\\](.*)";

The look-ahead pattern and the following pattern match at the same
position: (?!\S+) matches the empty string between the \[ and
something that _fails_ to match \S+ at that position, and that
something should start with the \]. Where can this happen?

Positive lookahead:

String re = "(.*)\\[(?=\\S+)\\](.*)";

The look-ahead pattern and the following pattern match at the same
position: (?=\S+) matches the empty string between the \[ and before
an \S+, and that \S+ should start with the \]. Where can this happen?

(Javadoc for 1.4.2 was not too helpful here, so I experimented a bit,
never having used these myself.)
.