如何从R中的特定字符之前的字符串中删除所有文本?

有时我们想从一个大字符串中提取一个子字符串,并且该子字符串位于特定字符之后。例如,字符串可以是“ Learning.Computer.Science.is.not.difficult-Author”,我们想从中提取“作者”一词。这可以借助gsub函数来完成。

例子

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"
猜你喜欢