Search code examples
rstrsplitstringr

strsplit by spaces greater than one in R


Given a string,

mystr = "Average student score       88"

I wish to split if there are more than 1 space. I wish to obtain the following:

"Average student score" "88"

I searched that "\s+" will split by any number of spaces.

strsplit(mystr, "\\s+")

But this is not what I want. Is there any option within strsplit that can split strings based on a certain number of spaces (say space = k) or a rule on spaces (say space > 1)?


Solution

  • You may specify it through a repetition quantifier.

    strsplit(mystr, "\\s{2,}")
    

    \\s{2,} regex should match two or more spaces.