in reply to replacing code with regex
Seems a lot more robust than this solution - well, except if you can't trust the input, ofcourse.
update: never mind, I misread your question.