Removing Whitespace With Perl

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Removing Whitespace With Perl

Post by Grim... »

Grr! :evil: ARGH!

Stoopid Perl! Stoopid whitespace!

I need to remove whitespace from my string, but not newlines, or spaces between words.

So that

Code: Select all

this is
                    my string
becomes

Code: Select all

this is
my string
Any ideas?

<edit>
The closest I've come is

Code: Select all

sub trimwhitespace($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}
but that just takes the whitespace from the beginning or end of the string, not the middle :(
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Well, I did it by splitting the string into an array at newlines and then running that trimwhitespace fucntion - the code:

Code: Select all

sub killwhitespace ($)
{
	my $string = shift;
	my @pa = split(/\n/, $string );
	foreach (@pa){
		$_ = trimwhitespace($_);
	} 
	$string = join("\n", @pa); 

	return $string ;
}

sub trimwhitespace($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

This works in the Regex Coach.

Code: Select all

s/[\t ]{2}//g
EDIT: Argh .. no it doesn't .. my test just happened to have odd numbers of spaces. It fails if there's an even number. Back to the drawing board.

EDIT 2: Right..

Code: Select all

s/[\t ]+/ /g
s/^ //mg
s/ $//mg
Shame it's 3 patterns rather than a nice elegant single pattern, but it does work (in the Regex Coach).
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

#!/bin/perl

$string = "
           some space here
        and a new line
     here          ";

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

print $string;
Had to make a post :)
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

won't this only remove whitespace from start & end of the string only :?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

n00b Saibot wrote:won't this only remove whitespace from start & end of the string only :?
No it's in "multiline" mode... ^ and $ now represent start and end of line ;)

EDIT | That's what the "m" modifier does
subychick88
Forum Newbie
Posts: 1
Joined: Wed May 26, 2010 12:49 pm

Re: Removing Whitespace With Perl

Post by subychick88 »

I found the answer to removing white space in the middle of an array in Perl!

All you need is this regular expression:
s/\s+/ /g;

This is my code:
foreach $OUTPUTFILE (@OUTPUTFILE)
{
#removes white space from the middle of strings and replaces it with commas
$OUTPUTFILE =~ s/\s+/,/g;
print OUTPUTFILE ($OUTPUTFILE);
}

I found it on this website:
http://www.nntp.perl.org/group/perl.beg ... 01365.html
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Removing Whitespace With Perl

Post by John Cartwright »

You do realize this thread is 5 years old. :roll:
Post Reply