I have been trying to solve this little issue for almost 2 hours, but without success. I simply want to separate a string by the delimiter: one space followed by any character. In the second element I want to keep the delimiter, whereas in the first element it shall not appear. Example:
x <- "123123 123 A123"
"123123 123" "A123"
If you want to follow your approach, you need to match 1+ whitespaces followed (i.e. you need a lookahead here) with a letter to consume the whitespaces:
> strsplit(x,"\\s+(?=[A-Z])",perl=T) []  "123123 123" "A123"
See the PCRE regex demo.
\s+- 1 or more whitespaces (put into the match value and thus will be removed during splitting)
(?=[A-Z])- the uppercase ASCII letter must appear immediately to the right of the current location, else fail the match (the letter is not part of the match value, and will be kept in the result)
You may also match up to the last non-whitespace char followed with 1+ whitespaces and use
\K match reset operator to discard the match before the whitespace:
> strsplit(x,"^.*\\S\\K\\s+",perl=T) []  "123123 123" "A123"
If the string contains line breaks, add a DOTALL flag since a dot in a PCRE regex does not match line breaks by default:
^- start of string
.*- any 0+ chars up to the last occurrence of the subsequent subpatterns (that is,
\\S- a non-whitespace
\\K- here, drop all the text matched so far
\\s+- 1 or more whitespaces.