Reputation: 21012
I am trying to split a string with multiple white spaces. I only want to split where there are 2 or more white spaces. I have tried multiple things and I keep getting the same output which is that it splits after every letter. Here is the last thing I tried
@cellMessage = split(s/ {2,}//g, $message);
foreach(@cellMessage){
print "$_ \n";
}
Upvotes: 6
Views: 41942
Reputation: 11
You can actually do:
@cellMessage = split(/\s+/, $message);
It does the same thing as " @cellMessage = split(/ {2,}/, $message);" but looks a little cleaner to me imo.
Upvotes: 0
Reputation: 62037
use strict;
use warnings;
use Data::Dumper;
# 1 22 333
my $message = 'this that other 555';
my @cellMessage = split /\s{2,}/, $message;
print Dumper(\@cellMessage);
__END__
$VAR1 = [
'this that',
'other',
'555'
];
Upvotes: 3
Reputation: 4146
Keeping the syntax you used in your example I would recommend this:
@cellMessage = split(/\s{2,}/, $message);
foreach(@cellMessage){
print "$_ \n";
}
because you will match any whitespace character (tabs, spaces, etc...). The problem with your original code was that the split
instruction is looking for a pattern and the regex you provided was resulting in the empty string //
, which splits $message
into individual characters.
Upvotes: 9
Reputation: 8190
Try this one: \b(\s{2,})\b
That should get you anything with multiple spaces between word boundries.
Upvotes: -1