in reply to Is there a way to make these two regex lines cleaner?

For the first line: most characters do not need escaping in a character class, probably only '-' and ']'.

For the second line: coming soon to a perl near you:

% perl -wle 'use builtin qw{trim}; $s=" foo bar "; $s=trim($s); print +"<$s>"' Built-in function 'builtin::trim' is experimental at -e line 1. <foo bar> %