The question of how to find every match when they might overlap was asked in Overlapping matches in Regex. However, as far as I can see, the answers there does not cover a more general case.
How can we find all substrings that begin with "a"
and end with "z"
? For example, given "akzzaz"
, it should find "akz"
, "akzz"
, "az"
and "akzzaz"
.
Since there may be more than one match starting at the same position, ("akz"
and "akzz"
) and also there may be more than one match ending at the same position ("az"
and "akzzaz"
) I cannot see how using a lookahead or lookbehind helps as in the mentioned link. (Also, please bear in mind that in the general case "a"
and "z"
might be more complex regular expressions)
I use C#, so, in case it matters, having any feature specific to .Net Regular Expressions is OK.