How to remove all text from a string before a particular character in R?

R ProgrammingServer Side ProgrammingProgramming

Sometimes we want to extract a sub-string from a big string and that sub-string lies after a particular character. For example, a string could be “Learning.Computer.Science.is.not.difficult-Author” and we want to extract the word Author from it. This can be done with the help of gsub function.

Examples

x1<-"Learning.Computer.Science.is.not.difficult-Author"
gsub(".*-","",x1)
[1] "Author"
x2<-"Learning.Computer.Science.is.not.difficult Author"
gsub(".* ","",x2)
[1] "Author"
x3<-"Learning.Computer.Science.is.not.difficult&Author"
gsub(".*&","",x2)
[1] "Learning.Computer.Science.is.not.difficult Author"
x3<-"Learning.Computer.Science.is.not.difficult^Author"
x3<-"Learning.Computer.Science.is.not.difficult&Author"
gsub(".*&","",x3)
[1] "Author"
x4<-"Learning.Computer.Science.is.not.difficult%Author"
gsub(".*%","",x4)
[1] "Author"
x5<-"Learning.Computer.Science.is.not.difficult#Author"
gsub(".*#","",x5)
[1] "Author"
x6<-"Learning.Computer.Science.is.not.difficult@Author"
gsub(".*@","",x6)
[1] "Author"
x7<-"Learning.Computer.Science.is.not.difficult~Author"
gsub(".*~","",x7)
[1] "Author"
x8<-"Learning.Computer.Science.is.not.difficult/Author"
gsub(".*/","",x8)
[1] "Author"
x9<-"Learning.Computer.Science.is.not.difficult;Author"
gsub(".*;","",x9)
[1] "Author"
x10<-"Learning.Computer.Science.is.not.difficult:Author"
gsub(".*:","",x10)
[1] "Author"
x11<-"Learning.Computer.Science.is.not.difficult< Author"
gsub(".*< ","",x11)
[1] "Author"
x12<-"Corona Virus has changed the world -Nizam"
gsub(".*-","",x12)
[1] "Nizam"
x13<-"Corona Virus has changed the world :Nizam"
gsub(".*:","",x13)
[1] "Nizam"
raja
Published on 21-Aug-2020 12:04:53
Advertisements