http://qs321.pair.com?node_id=613271


in reply to Re: Using regex in Map function
in thread Using regex in Map function

or, even better, this
That may be subject to judgement.

While your 1st and 3rd variant append behind the line-ending newline, the 2nd is probably closer to what the OP wanted, since $ is matching before that newline.