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

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.