5This is actually the most natural approach. I avoided while loops initially, when only for loops had been discussed. It is redundant in the original approach, however, to find every instance of '%(' to count the number of repetitions and then find them all again when extracting the cue keys. A more natural way to control the loop is a while loop stopping when there are no further occurrences of '%('. This involves some further adjustments. You must cut the loop in a different place (to end after searching for '%(') . As discussed before, cutting a loop in a different place may require changes before and after the loop, too.