Remove complicated regex with rtrim() call and add tests for it