r语言 - 从字符串中删除三个连续的点和以下数字



我有以下数据:

myvec <- c("Some 1 sentence...113_the answer", "Some 3 sentence...1_the answer")

我想从这些字符串中删除三个连续的点和下面的数字,我应该怎么做?

I could do:

myvec <- gsub("\...", "", myvec)

然后用regex删除数字,但这对我的数据有点不安全(因为字符串中可能有更多的数字,我需要保持在创建的示例中)。

我应该如何删除三个点和一个数字的确切组合?

编辑期望输出值:

myvec <- c("Some 1 sentence_the answer", "Some 3 sentence_the answer")

你可以做

gsub('\.{3}\d{1}', '', myvec)
# [1] "Some 1 sentence13_the answer" "Some 3 sentence_the answer"  

gsub('\.{3}.*_', ' ', myvec)
[# 1] "Some 1 sentence the answer" "Some 3 sentence the answer"

取决于你是想删除一个数字还是整个数字。

library(stringr)

myvec <- c("Some 1 sentence...113_the answer", "Some 3 sentence...1_the answer") str_remove_all(myvec,'\.\.\.\d')

str_remove_all(myvec,'\.\.\.\d{1,20}')

d{1,20}表示您希望删除以下所有从1到20的数字

最新更新