niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Perl
FAQ 4.32 How do I strip blank space from the beginning/end of a string?
Post new topic   Reply to topic Page 1 of 1 [1 Post] View previous topic :: View next topic
Author Message
PerlFAQ Server
*nix forums Guru


Joined: 09 Apr 2005
Posts: 1008

PostPosted: Fri Jul 21, 2006 1:03 pm    Post subject: FAQ 4.32 How do I strip blank space from the beginning/end of a string? Reply with quote

This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.32: How do I strip blank space from the beginning/end of a string?

(contributed by brian d foy)

A substitution can do this for you. For a single line, you want to
replace all the leading or trailing whitespace with nothing. You can do
that with a pair of substitutions.

s/^\s+//;
s/\s+$//;

You can also write that as a single substitution, although it turns out
the combined statement is slower than the separate ones. That might not
matter to you, though.

s/^\s+|\s+$//g;

In this regular expression, the alternation matches either at the
beginning or the end of the string since the anchors have a lower
precedence than the alternation. With the "/g" flag, the substitution
makes all possible matches, so it gets both. Remember, the trailing
newline matches the "\s+", and the "$" anchor can match to the physical
end of the string, so the newline disappears too. Just add the newline
to the output, which has the added benefit of preserving "blank"
(consisting entirely of whitespace) lines which the "^\s+" would remove
all by itself.

while( <> )
{
s/^\s+|\s+$//g;
print "$_\n";
}

For a multi-line string, you can apply the regular expression to each
logical line in the string by adding the "/m" flag (for "multi-line").
With the "/m" flag, the "$" matches *before* an embedded newline, so it
doesn't remove it. It still removes the newline at the end of the
string.

$string =~ s/^\s+|\s+$//gm;

Remember that lines consisting entirely of whitespace will disappear,
since the first part of the alternation can match the entire string and
replace it with nothing. If need to keep embedded blank lines, you have
to do a little more work. Instead of matching any whitespace (since that
includes a newline), just match the other whitespace.

$string =~ s/^[\t\f ]+|[\t\f ]+$//mg;



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

--
Posted via a free Usenet account from http://www.teranews.com
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [1 Post] View previous topic :: View next topic
The time now is Sat Nov 22, 2008 9:17 pm | All times are GMT
navigation Forum index » Programming » Perl
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts FAQ 4.34 How do I extract selected columns from a string? PerlFAQ Server Perl 0 Fri Jul 21, 2006 7:03 am
No new posts print all permutations of string anurag C 10 Thu Jul 20, 2006 5:57 pm
No new posts converting array values to monomaniac21 PHP 11 Thu Jul 20, 2006 10:17 am
No new posts Depricated String Functions in Python Anoop python 14 Thu Jul 20, 2006 6:26 am
No new posts how to rewind to file beginning immediately after writing... Rajorshi Biswas C 3 Thu Jul 20, 2006 5:59 am

Remortgages | Loans | Online advertising | Ringtone | Xbox Mod Chip
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1149s ][ Queries: 16 (0.0466s) ][ GZIP on - Debug on ]