XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).
Moderator: General Moderators
Grim...
DevNet Resident
Posts: 1445 Joined: Tue May 18, 2004 5:32 am
Location: London, UK
Post
by Grim... » Fri Dec 02, 2005 4:42 am
Grr!
ARGH!
Stoopid Perl! Stoopid whitespace!
I need to remove whitespace from my string, but not newlines, or spaces between words.
So that
becomes
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... » Fri Dec 02, 2005 5:01 am
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;
}
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Fri Dec 02, 2005 6:08 am
This works in the Regex Coach.
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..
Shame it's 3 patterns rather than a nice elegant single pattern, but it does work (in the Regex Coach).
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Fri Dec 02, 2005 6:26 am
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
n00b Saibot
DevNet Resident
Posts: 1452 Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:
Post
by n00b Saibot » Sat Dec 03, 2005 12:17 am
won't this only remove whitespace from start & end of the string only
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Sat Dec 03, 2005 7:39 am
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
Post
by subychick88 » Wed May 26, 2010 1:09 pm
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