Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split column by last word in sentence

YARQ (Yet another regex question).

How would I go about splitting the following into two columns, making sure that the last column contains the last word in the sentence and the first column contains everything else.

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

Such that I end up with:

col1                         col2
this is a                    test
Testing 1,2,3                Hello
Foo                          Bar
Random 214274(%*(^(*         Sample
Some                         Hyphenated-Thing
like image 615
Brandon Bertelsen Avatar asked Mar 21 '13 04:03

Brandon Bertelsen


People also ask

How do I separate the last text in Excel?

Select the cell or column that contains the text you want to split. Select Data > Text to Columns.


1 Answers

This looks like a job for look ahead. We'll find spaces followed by things which are not spaces.

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"
like image 135
sebastian-c Avatar answered Oct 13 '22 01:10

sebastian-c