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

Q&A > regular expressions > How do I remove whitespace at the beginning or end of my string? - Answer contributed by gridlock

You want to do a

s/^\s+//

followed by a

s/\s+$//

Replies are listed 'Best First'.
Re: Answer: How do I remove whitespace at the beginning or end of my string?
by PERLscienceman (Curate) on Feb 21, 2004 at 12:57 UTC
    ...or... more simply, you could do it this way as a one liner:
    s/^\s+|\s+$//g