简体   繁体   中英

extract words in between two commas in R

I have the following string

word <- 'Fu Tien Mansion, Taikoo Shing, Hong Kong'

and I want to extract the word in between the two commas and concatenate it with the first word, what regex to use?

Desired output:

'Taikoo Shing Fu Tien Mansion' 

We can use sub to capture substrings as a group and in the replacement use backreferences of that group

sub("^([^,]+),\\s*([^,]+),.*", "\\2 \\1", word)
#[1] "Taikoo Shing Fu Tien Mansion"
> x <- strsplit(word, ",")[[1]]
> paste(x[2], x[1])
[1] " Taikoo Shing Fu Tien Mansion
sapply(word, function(w){
    ind = gregexpr(",", w)[[1]]
    paste(substring(w, ind[1] + 2, ind[2] - 1),
          substring(w, 1, ind[1] - 1))
})
#Fu Tien Mansion, Taikoo Shing, Hong Kong 
#          "Taikoo Shing Fu Tien Mansion" 

You can also use sapply with strsplit -

> paste(trimws(sapply(strsplit(word,","), `[`, 2)), trimws(sapply(strsplit(word,","), `[`, 1)))
[1] "Taikoo Shing Fu Tien Mansion"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM