Use stringi::str_replace_all
:
library(stringi)
data <- c("I am [female name]. I am ten years old", "My father is [male name][surname]", "I went to school today")
remove_us <- c("[female name]","[male name]","[surname]")
stri_replace_all_fixed(data, remove_us, "", vectorize_all=FALSE)
Results
[1] "I am . I am ten years old" "My father is " "I went to school today"
See R proof.
However, it is simpler with gsub
:
gsub('\[[^][]*]', '', data)
See another R proof.
--------------------------------------------------------------------------------
[ '['
--------------------------------------------------------------------------------
[^][]* any character except: ']', '[' (0 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
] ']'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…