Rather than trim, you could modify your matching regex to ignore whitespace at the start/end.