Regex defined zero-width assertions

Regex defined zero-width assertions use ( and ) and there is a ? after the opening (. There are two assertions: positive, denoted by the = character, and negative, denoted by the ! character. If the assertion is backward-looking then the ? character is followed by a < character. Thus, (?=...) is a positive lookahead assertion and (?<!...) is a negative lookbehind assertion.

(?=pattern)

The positive lookahead assertion ensures that the string matches the pattern after the current position. For example, abc(?=K) ensures that the characters "abc" in the checked strings are followed by the letter "K", but this check does not consume the character "K".

(?!pattern)

The negative lookahead assertion ...

Get Java 9 Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.