The difference is that the
while construct will continue to loop so long as the condition is true. Which in this case is once for everytime the regex is true. So in this case 3 times. On the other hand the
for loop will construct its list and then go through once for each item in list. In this case the list is constructed of the items in $1, and $2 (because of the two capturing parens.) each time, for a total of six items, and hence the difference in the results.
This can clearly be shown (when the regex execute and how many times in each loop) if you add something like use re 'debug'; at the top of you code.
-enlil