Refactor perl regex find/replace to PSED function