Stefhan
Stefhan

Reputation: 610

How to group only words, no spaces?

I want group all words without white space e.g.:

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

Upvotes: 2

Views: 2832

Answers (5)

Vasyl Gutnyk
Vasyl Gutnyk

Reputation: 5039

var reg = /([\w$])+/gi; // matches only words without space and noalphabetic characters

Upvotes: 0

FatherStorm
FatherStorm

Reputation: 7183

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

Upvotes: 1

CanSpice
CanSpice

Reputation: 35798

In Perl:

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

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

Upvotes: 3

Jack
Jack

Reputation: 133577

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.

Upvotes: 0

splash
splash

Reputation: 13327

(\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"

Upvotes: 3

Related Questions