Getting overlapping regex matches in C#

You are already selecting the 1 in front of the second zero by the first match.

100001 0001
^^^^^^

This is the first match. The rest is just 0001 which does not match your regex.


You can circumvent this behavior if you are using lookaheads/lookbehinds:

(?<=1)(0*)(?=1)

Live example


Because you cannot use lookbehinds in JavaScript, it is enough to only use one lookahead, to prevent the overlapping:

1(0*)(?=1)

Live example


And a hint for your regex101 example: You did not add the global flag, which prevents more than one selection.

Leave a Comment