I have data as follows:
myvec <- c("Some 1 sentence...113_the answer", "Some 3 sentence...1_the answer")
I would like to remove the three consecutrive dots and the following number from these strings, how should I do this?
I could do:
myvec <- gsub("\\...", "", myvec)
And then remove the numbers with regex, but this is a bit unsafe for my data (as there might be more numbers in the string that I need to keep as in the created example).
How should I remove the exact combination of three dots and a number?
CodePudding user response:
library(stringr)
myvec <- c("Some 1 sentence...113_the answer", "Some 3 sentence...1_the answer") str_remove_all(myvec,'\\.\\.\\.\\d')
or
str_remove_all(myvec,'\\.\\.\\.\\d{1,20}')
d{1,20}
means that you want to delete all following numbers if their number is from one to 20
CodePudding user response:
You could either do
gsub('\\.{3}\\d{1}', '', myvec)
# [1] "Some 1 sentence13_the answer" "Some 3 sentence_the answer"
or
gsub('\\.{3}.*_', ' ', myvec)
[# 1] "Some 1 sentence the answer" "Some 3 sentence the answer"
depending if you want to remove one digit or the entire number.