O'Reilly logo

Java 9 Regular Expressions by Anubhava Srivastava

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required