从字符串中删除初始(匹配)模式

时间:2020-03-03 17:12:37

标签: r regex

我有以下字符串向量:

v<-c("RT @name1: hello world", "Hi guys, how are you?", "Hello RT I have no text", "RT @name2: Hello!")

我只想删除位于字符串开头的RT并将结果存储在另一个向量中,例如w

> w
 "@name1: hello world"    "Hi guys, how are you?"     "Hello RT I have no text"     "@name2: Hello!"

也许我可以使用软件包str_extract_all中的函数stringr,但不能将其应用于问题。

2 个答案:

答案 0 :(得分:1)

使用gsub和'anchor'^,这表示字符串的开头:

w <- gsub("^RT\\s", "", v)

答案 1 :(得分:0)

<-  str_replace(v,"^RT","")
相关问题