On Thu, 4 May 2000, Bill Stennett wrote:

> Hi all
> 
> sorry for this probably very basic question but i'd appreciate a little
> help!
> 
> I need to remove spaces from user input at the start and end of the string.
> I can use:
> 
>    $varname=3D~s/^\s+(.*)/$1/;    # remove leading spaces
> 
> to remove spaces at the start of the string and this works OK but my
> attempt to remove
> spaces from the end does not work:
> 
>    $varname=3D~s/(.*)\s+$/$1/; # remove trailing spaces?
> 
> (presunmably this is because (.*) matches everything includng the spaces?)
> 
> Can anyone suggest the best way to clean up the user input. - I wish to
> allow spaces
>  in the middle of the string and remove all whitespace from the start and
> end.
>

That's because you used the "greedy" mode to match any characters
at the beginning so you could include into $1 all but one last
space and stil match. If you want to remove all trailing spaces I think
a better regex would be: $varname=~s/(.*?)\s+$/$1/; # remove trailing  spaces
Probably even better would be:  $varname=~s/\s+$//;

**** [EMAIL PROTECTED] <Carl Jolley>
**** All opinions are my own and not necessarily those of my employer ****


---
You are currently subscribed to perl-win32-users as: [archive@jab.org]
To unsubscribe, forward this message to
         [EMAIL PROTECTED]
For non-automated Mailing List support, send email to  
         [EMAIL PROTECTED]

Reply via email to