按句子中的最后一个单词拆分列

Bra*_*sen 5 regex r text-segmentation

YARQ(另一个正则表达式问题).

我如何将以下内容拆分为两列,确保最后一列包含句子中的最后一个单词,第一列包含其他所有单词.

x <- c("This is a test",
       "Testing 1,2,3 Hello",
       "Foo Bar",
       "Random 214274(%*(^(* Sample",
       "Some Hyphenated-Thing"
       )
Run Code Online (Sandbox Code Playgroud)

这样我最终得到:

col1                         col2
this is a                    test
Testing 1,2,3                Hello
Foo                          Bar
Random 214274(%*(^(*         Sample
Some                         Hyphenated-Thing
Run Code Online (Sandbox Code Playgroud)

seb*_*n-c 9

这看起来像是一个展望未来的工作.我们会找到空格,后面跟不是空格的东西.

split <- strsplit(x, " (?=[^ ]+$)", perl=TRUE)
matrix(unlist(split), ncol=2, byrow=TRUE)

     [,1]                   [,2]              
[1,] "This is a"            "test"            
[2,] "Testing 1,2,3"        "Hello"           
[3,] "Foo"                  "Bar"             
[4,] "Random 214274(%*(^(*" "Sample"          
[5,] "Some"                 "Hyphenated-Thing"
Run Code Online (Sandbox Code Playgroud)