What do you exactly want? The above code produces the starting position and length of a sequence of non-overlapping matches according to the above pattern. It looks like you rather want a minimal and maximal match length for each possible starting position of a match. Is that correct? If no, please clarify the rule according to which the program should behave and I'll try my best to help you.