Reputation: 12178
I want to split a string by everything but whitespaces.
Example:
Assume that I have a string:
"The quick brown fox jumps over the lazy dog"
I want to split it and get the following list of space-strings:
[" ", " ", " ", " ", " ", " ", " ", " "]
Can you please show me how to do it?
Thanks!
Upvotes: 0
Views: 313
Reputation: 7948
why not just look for the spaces instead of splitting by non-space characters?
pattern = (\s+)
Demo
Upvotes: 0
Reputation: 70732
You can use \S
to split by here which means match any non-white space character.
my @list = split /\S+/, $string;
Or better yet, just match your whitespace instead of having to split.
my @list = $string =~ /\s+/g;
Upvotes: 2
Reputation: 42099
\S
splits on any non-whitespace.
However, if you want to include tabs and newlines, then you should use (single character space). More than likely what you want is the
\S
as hwnd has provided.
my $string = q{The quick brown fox jumps over the lazy dog};
my @values = split /[^ ]+/, $string;
Upvotes: 1