3

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)?

user2498497
  • 693
  • 2
  • 14
  • 22

1 Answers1

13

You may specify it through a repetition quantifier.

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

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

thelatemail
  • 91,185
  • 12
  • 128
  • 188
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Thank you! Is there an option for matching an exact number of spaces too? – user2498497 Jan 21 '16 at 05:44
  • 6
    ya, remove the comma. `\\s{2}` for matching exactly two spaces,For 3, it would be `\\s{3}`, and to match min of 3 and max of 4 then it would be `\\s{3,4}` – Avinash Raj Jan 21 '16 at 05:45