BTW, although %k is not lexicalized to the sub, I don't think it matters, since any leftover contents which are not overwritten won't be used.
(Also assume that no other characters are present in the words.)

Update: I added /s to both substitutions, to avoid problems with \n. Thanks tilly!