Re: Pulling white space off before/after string?
by bobf (Monsignor) on Apr 17, 2007 at 04:33 UTC
|
s/^\s+//; # strip white space from the beginning
s/\s+$//; # strip white space from the end
| [reply] [d/l] |
|
I just came back to post I found a working solution:
$buff =~ s/ *$//; #Remove Trailing Spaces
$buff =~ s/^ *//; #Remove Leading Spaces
You've got the same idea I have, but I'm too regex newbie to figure out the differences. Can you tell me if there are any differences in our regexes?
Thanks!!
| [reply] [d/l] |
|
/ *$/ means 'match zero or more space characters anchored to the end of the string', which will always match since it can always match zero characters. It will also match only space characters - other white space (such as tabs) will not match.
/\s+$/ means 'match one or more white space characters anchored to the end of the string'. The \s includes spaces, tabs, and other forms of white space.
See perlre for more information. The Tutorials section also has several entries in Pattern Matching, Regular Expressions, and Parsing, which you may find helpful.
| [reply] [d/l] [select] |
|
| [reply] |
Re: Pulling white space off before/after string?
by GrandFather (Saint) on Apr 17, 2007 at 04:35 UTC
|
Perhaps the biggest and most generally used tool in the Perl toolbox is the regex (see also perlretut). I strongly recommend that you take a look at the regular expression documentation then if you are still asking this question come back with the regex substitution you are having trouble with.
DWIM is Perl's answer to Gödel
| [reply] |
Re: Pulling white space off before/after string?
by monkey_boy (Priest) on Apr 17, 2007 at 09:48 UTC
|
use String::Util qw(trim)
my $s = trim(' here are lots of nasty spaces ');
UPDATE: seems i accidently caused a small argument with my tounge in cheek comments. However, IMHO this is a nice little module, with the trim() function being one of many nice little snipits provided that i use often, worth checking out!. Also, just becuase it hasnt been updated in a year does not make it a obsolete, its more likely that there are no bugs to be fixed, as all the subs are quite small & simple.
This is not a Signature...
| [reply] [d/l] |
|
#---------------------------------------------------------------------
+---------
# trim
#
=head1 trim(string)
Returns the string with all leading and trailing whitespace removed.
Trim on undef returns undef.
=cut
sub trim{
my ($val) = @_;
if (defined $val) {
$val =~ s|^\s+||s;
$val =~ s|\s+$||s;
};
return $val;
}
#
# trim
#---------------------------------------------------------------------
+---------
If it really bothers you just add one line to your code and be done with it.
| [reply] [d/l] |
|
# trim whitespace
$string =~ s/^\s+//;
$string =~ s/\s+$//;
... to:
trim( $string ); | [reply] [d/l] [select] |
|
Re: Pulling white space off before/after string?
by f00li5h (Chaplain) on Apr 17, 2007 at 04:36 UTC
|
Checkout perlfaq4, you may also want to have a look at perlre and perlretut
The size of the strings may change the way you do it, because capturing a 10Gb string just to remove spaces from the start and end may not be the best approach.
Also, what have you tried so far?
@_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
Much Later removed typos
| [reply] [d/l] |
Re: Pulling white space off before/after string?
by swampyankee (Parson) on Apr 17, 2007 at 04:43 UTC
|
s/^\s+//; # removes leading white space
s/\s+$//; # removes trailing white space
emc
Insisting on perfect safety is for people who don't have the balls to live in the real world.
—Mary Shafer, NASA Dryden Flight Research Center
| [reply] [d/l] |
Re: Pulling white space off before/after string?
by swares (Monk) on Apr 17, 2007 at 06:05 UTC
|
If you want to make the changes on the fly you could use this with your regex. The .bak specifies a backup file extension, so you have an original copy. You can change it as you like or remove it if you wish no backup. The nice part is both operations are performed at once. You could also do that like above as so by looping through the lines of the file.
Variable Substitution
$my_data_line =~ s/^\s+|\s$+//g;
Modify file inplace and make a backup copy to filename.html.bak the easy way.
perl -p -i.bak -e 's/^\s+|\s$+//g' *.html
| [reply] [d/l] [select] |
|
$my_data_line =~ s/^\s+|\s+$//g;
| [reply] [d/l] |