implemented memory based pattern replace