我想删除 'sat' 和之后的任何内容,但不是 'saturday'。虽然这看起来很简单,但我一直无法找到关于此的主题。
例子:
text <- c("good morning amer sat","this morning saturday")
期望的结果:
"good morning amer","this morning saturday"
我所做的一切都会删除星期六。
最佳答案
我们可以使用单词边界 (\\b
)
sub("\\s*\\bsat\\b", "", text)
-输出
[1] "good morning amer" "this morning saturday"
或者用 stringr
library(stringr)
str_remove(text, "\\s*\\bsat\\b")
[1] "good morning amer" "this morning saturday"
关于删除特定字符串和之后的任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72747104/