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]stackoverfl开发者_JAVA百科owi know its a easy regex but i forgot how i do that xP.
(\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"
In Perl:
my $str = "I like stackoverflow";
my @words = split '\s+', $str;
@words
now contains "I", "like", and "stackoverflow".
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
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.
var reg = /([\w$])+/gi; // matches only words without space and noalphabetic characters
精彩评论