tags:

views:

45

answers:

4

Hi, i want group all words without white space e.g.:

I like stackoverflow
[0]I
[1]like
[2]stackoverflow

i know its a easy regex but i forgot how i do that xP.

A: 

Usually you can use \w for an alphanumeric character so if you don't have strange symbols you can just go with something like

(\w)+\s+(\w)+\s+.....

where \s means any whitespace character

If you have words that are made also by symbols (even if it's quite nonsense) you can use \S instead that \w to match everything except white space.. but if you have just a list of words separated by spaces you can define a set of delimiters and split the string with an API function.

Jack
with this regex will match whitespace and, will group limited number of words
Stefhan
+1  A: 

What language are you using?

PHP: $array=array_filter(array_map('trim',explode(" ",$string)));

or better yet:

or better yet. $array=array_filter(array_map("trim",explode(" ",preg_replace("/[^a-zA-Z0-9\s]/", "", $copy))));

In action at one of my dev sites

FatherStorm
i need a group without white space
Stefhan
THis will: Split on spaces, trim out any doublespaces, drop out any empty indexes in outward working order.
FatherStorm
+2  A: 
(\w+)\s+(\w+)\s+(\w+)

In Java you would use something like this

String[] splits = "I like stackoverflow".split("\\s+");
// split[0] = "I"
// split[1] = "like"
// split[2] = "stackoverflow"
splash
i need a group without white space, and without limit
Stefhan
That's what he wrote, with `split` you don't have any limit..
Jack
@Stefhan, with pure regular expressions you can't group without limit. Every group must be known before. The split function does a repeated regex match.
splash
+3  A: 

In Perl:

my $str = "I like stackoverflow";
my @words = split '\s+', $str;

@words now contains "I", "like", and "stackoverflow".

CanSpice