My problem is:
Given a (multiline) string, I want a function which remove all leading and trailing
white-space (as given by \s).
Examples:
" abbds \n sass " => "abbds \n sass"
" a " => "a"
" \n first phrase \n second phrase \n \n" => "first phrase \n second phrase"
" \017 a " => "\017 a"
" " => ""
These also show the following cases:
* space in between goes on unchanged,
* also characters which are not space and which does not match \w (like \017), must
remain unchanged too,
* strings with only spaces map into the empty string
I have tried
sub trim {
my ($text) = @_;
$text =~ m<^\s*(?=\S)(.*)(?=\S)\s*$>s;
return $2;
}
The rationale is: (1) scan leading spaces, (2) match a zero-length assertion when
a non space is encountered, (3) scan everything collecting into $2 until
(4) a zero-length assertion for a non space is matched, (5) followed
by trailing spaces. Then I catch $2.
But it does not work. If I change the pattern to
^\s*\b(.*)\b\s*$
and return $1, it works except for cases like " \017 a ".
I'm not sure I have understood how to use (?= ). Is \b equivalent
to (?=\w) ?
I would like the solution as concise and simple as possible.
Thanks for any help.
Adriano
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]