Reputation: 5306
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
?
Upvotes: 1
Views: 414
Reputation: 89639
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.
Upvotes: 5