我想删除"sat"和"周六"之后的任何内容,但不想删除"周六".虽然这看起来很简单,但我一直找不到这方面的线索.
例子:
text <- c("good morning amer sat","this morning saturday")
预期结果:
"good morning amer","this morning saturday"
我做的每件事都会取消星期六.
我想删除"sat"和"周六"之后的任何内容,但不想删除"周六".虽然这看起来很简单,但我一直找不到这方面的线索.
例子:
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"