I thought this kind of script had been done before, and you could do this with regsubex in one line...