I don't think anyone has yet explained
why the
while (//g) {} solution isn't working. The problem lies in the assignment to
$line, which resets
pos(). So, if there is a hit, the while loop will never terminate
†, it will find the same hit over and over again, each time adding a new pair of brackets.
†Well, because $line will grow two characters each iteration, eventually the program will run out of memory, terminating the program (and hence, the loop).