Dan Anderson wrote:
> 
> Is there a way to chomp all whitespace both at the beginning and end of
> a string?

That is what is known as a Frequently Asked Question or FAQ.  Perl
provides copious amounts of documentation including a large list of FAQs
in the perlfaq.pod file.  Perl also provides a program which can be used
to search the FAQs called "perldoc".

perldoc perldoc
perldoc perl
perldoc perlfaq
perldoc -q "strip blank space"


> I was thinking of using a regexp like s[^\s*?][]sg and
> s[\s*?$][]sg;  Is there a better way?

You are on the right track.  Using the substitution operator twice for
the beginning and end of the string is the usual way to do it, however
you have used the /s and /g options.  The /s option affects the
behaviour of . in a regex.  Since you are not using . in the regex this
option has no effect.  The /g option means that you want the pattern to
match globally, in other words, match as many times as the pattern
exists in the string, however both regexes are anchored at the beginning
and end respectively so they can only match once so the /g option has no
effect.  And finally, you are using the * modifier on the \s character
class which means that the substitution is performed on every string,
whether there is whitespace or not.  In other words, using the string
'text', the zero whitespace at the beginning the string will be replaced
with nothing.  You should use the + modifier so that only strings that
actually have whitespace will be modified.



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to