Hmm, I was indeed not taking care of backwhacks.
But then, the method would not change that much. Also keep track of another variable, let's call it $escape. Set it to 1 if you find a \. Then, in the next iteration of the loop, if that character is a ( or ) and $escape != 0, ignore it. If it is a backslash, ignore it too. Then set $escape back to 0.