This just makes things unnecessarily more complicated, scain. The original version is absolutely equivalent to yours and shorter to write - thus easier to understand. $string =~ s/^\s*(.*?)\s*$/$1/;
Some further explanation: The starting \s* eats up all whitespaces (because its greedy). Then (.*?) starts capturing and the first character must be \S (or the end of the string for something matching /^\s*$/). Due to its non-greediness the (.*?) advances slowly one character at a time, always trying to match afterwards the rest of the pattern (\s*$) and backtracks if not successful. So all trailing whitespaces are for sure eaten up by the greedy \s* at the end of the pattern leaving a \S as the last character in the capturing brackets.
The solution with two replaces given by many other monks is preferable as it
$file =~ s/^\s+//mg; $file =~ s/\s+$//mg;
-- Hofmator
In reply to Re: Re: Dot star okay, or not?
by Hofmator
in thread Dot star okay, or not?
by Cirollo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |