http://www.perlmonks.org?node_id=443408


in reply to Re^4: trim leading & trailing whitespace
in thread trim leading & trailing whitespace

Danger, don't use \b if you want to detect the edges between spaces an nonspaces. It won't work for nonspace+nonword characters, like quotes and punctuation.

Perhaps replace the \b(.*?) with (\S.*?).

Still, you're not running this with warnings enabled, are you? Both $1 and $2 are optional, so both can be undef, but you use their value regardless. This runs without warnings:

s/^\s*?((?:\S.*?)?)\s*?(\n?)$/$1$2/gm;