Split a string into substrings on one or more spaces

I want to split a string into several substrings in those positions where one or more spaces occur (tab, space, ...). The documentationstrsplit() says that separation is interpreted as a regular expression.

So I tried the following, which did not work:

test = "123 nnn      dddddd"
strsplit(test, "[:space:]+")

he returns only:

[[1]]
[1] "123 nnn      dddddd"

but should return:

[[1]]
[1] "123" "nnn" "dddddd"

What is wrong in my code?

+5
source share
2 answers

Try

strsplit(test, '\\s+')
[[1]]
[1] "123"    "nnn"    "dddddd"

\\s will match all whitespace characters.

+9
source

[:space:] [], , .. [[:space:]]. [:space:] , :, s, p, a, c, e.

strsplit(test, "[[:space:]]+")

, strsplit POSIX ERE, [:space:].

PCRE ( Perl Compatible Regular Expression) [:space:] \p{Xps}. perl, .

(ASCII 32) \t \n, , (ASCII 32) :

strsplit(test, " +")
+7

All Articles