Click here to get back home

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

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 4.32 How do I strip blank space from the beginning/end of a string? PerlFAQ Server 02-26-2008
Get Chitika Premium
Posted by PerlFAQ Server on February 26, 2008, 9:03 am
Please log in for more thread options
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.

Similar ThreadsPosted
FAQ 4.32: How do I strip blank space from the beginning/end of a string? December 16, 2004, 6:03 pm
FAQ 4.32 How do I strip blank space from the beginning/end of a string? January 24, 2005, 6:03 am
FAQ 4.32 How do I strip blank space from the beginning/end of a string? May 18, 2005, 5:03 pm
FAQ 4.32 How do I strip blank space from the beginning/end of a string? August 3, 2005, 4:03 pm
FAQ 4.32 How do I strip blank space from the beginning/end of a string? September 7, 2005, 4:03 pm
FAQ 4.32 How do I strip blank space from the beginning/end of a string? December 18, 2005, 5:03 am
FAQ 4.32 How do I strip blank space from the beginning/end of a string? April 16, 2006, 9:03 pm
FAQ 4.32 How do I strip blank space from the beginning/end of a string? May 31, 2006, 9:03 am
FAQ 4.32 How do I strip blank space from the beginning/end of a string? July 21, 2006, 9:03 am
FAQ 4.32 How do I strip blank space from the beginning/end of a string? October 27, 2006, 9:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap