Suppose I have a String NNNN.
The regex is N+N.
How to configure the matcher to let it return NNNN, NNN and NN since NNN and NN also match pattern N+N?
Suppose I have a String NNNN.
The regex is N+N.
How to configure the matcher to let it return NNNN, NNN and NN since NNN and NN also match pattern N+N?
You need to enclose your pattern in a lookahead and a capture group:
(?=(N+N))
The results are in the group 1.
Since the lookahead is a zero-width assertion, characters are not consumed by the pattern and can be "reuse" for the next match (from the next position in the string).
N N N N
x______________^ # first match
x_________^ # second match
x____^ # third match
x____^ is the content of the capture group and x is the start position.
Matcher.start() and Matcher.end().