If you want to preserve internal whitespace, try
^[\s]* #the start of the string followed by any number of spaces
(.*) #zero or more characters of anything, stuff into $1. This
+is the actual string we want.
(?<!\s) #look-behind assertion to get to the last non-whitespace ch
\s*$ #match whitespace until EOL
/$1/x; #replace with the (.*)
Update: If you value your CPU time, use one of the other regular expressions instead. I got caught up in using the .*, leading to the need for the look-behind assertion. Better answers can be found here. For the interested,
Benchmark: timing 100000 iterations of my_long_one, one_liner, two_lin
my_long_one: 6 wallclock secs ( 4.62 usr + 0.00 sys = 4.62 CPU) @ 2
one_liner: 3 wallclock secs ( 3.89 usr + 0.00 sys = 3.89 CPU) @ 256
two_liner: 1 wallclock secs ( 2.16 usr + 0.00 sys = 2.16 CPU) @ 462
my_long_one => s/^[\s]*(.*)(?<!\s)\s*$/$1/
one_liner => s/^\s+|\s+$//g
two_liner => s/^\s+//g; s/s+$//g
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||