tags:

views:

97

answers:

4

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";
                }
A: 

Try this one: \b(\s{2,})\b

That should get you anything with multiple spaces between word boundries.

AllenG
Shinjuo's split syntax was wrong. It has nothing to do with word boundaries. Erik has the right answer.
jmz
+7  A: 
@cellMessage = split(/ {2,}/, $message);
Erik
Awesome that worked great
shinjuo
But it is not really whitespace, only spaces. \s will also give you tabs:@cellMessage = split(/\s{2,}/, $message);
Erik
Another small refinement would be to use + instead of {2,}. The + means exactly that, "at least one of": split(/\s+/, $message);
Philippe A.
@Philippe A., he said he wanted to split on 2 or more spaces. That would be `/\s\s+/`, which is only 1 less character than `/\s{2,}/`. They both mean the same thing.
cjm
+4  A: 

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.

dls
+2  A: 
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'
        ];
toolic