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

Capturing text from overlapping matches

Lookahead patterns are also very useful for situations where we want to match and capture text from overlapping matches.

Let's consider the following input string as an example:

thathathisthathathatis 

Suppose that we need to count the occurrence of the string, that, in this input, including all overlapping occurrences.

Note that there are three independent that substrings in the input string, but there are two additional overlapping matches that we need to match and count. Here are the start-end positions of overlapping the substring that:

Positions 0-3 3-6 10-13 13-16 16-19 

A simple search using the regex that will give us a match count of three because we miss out all the overlapping matches. ...

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