一千萬個為什麽

搜索

刪除給定文本中某個字符後的字符串

I have a dataset like the one below. I want to remove all characters after the character ©. How can I do that in R?

data_clean_phrase <- c("Copyright © The Society of Geomagnetism and Earth", 
"© 2013 Chinese National Committee ")

data_clean_df <- as.data.frame(data_clean_phrase)

最佳答案

例如:

 rs<-c("copyright @ The Society of mo","I want you to meet me @ the coffeshop")
 s<-gsub("@.*","",rs)
 s
 [1] "copyright "             "I want you to meet me "

或者,如果你想保留@字符:

 s<-gsub("(@).*","\\1",rs)
 s
 [1] "copyright @"             "I want you to meet me @"

編輯:如果您想要刪除上一個@上的所有內容,您只需要使用相應的正則表達式執行前面的示例。例:

rs<-c("copyright @ The Society of mo located @ my house","I want you to meet me @ the coffeshop")
s<-gsub("(.*)@.*","\\1",rs)
s
[1] "copyright @ The Society of mo located " "I want you to meet me "

鑒於我們正在尋找匹配,sub和gsub將給你相同的答案。

轉載註明原文: 刪除給定文本中某個字符後的字符串