in reply to Avoid recursive replacement using regex
You have the wrong order of replacements. Do replacement 4|5 first; then 1 (2|3 are obtained for free).