You can simply use \\[ as an item for matching, you donβt need extra square brackets if you do not agree with several options:
> mystring <- 'abc[de' > gsub("\\[", "", mystring) [1] "abcde"
You can make it even easier and faster for single characters by removing the special meaning with fixed=TRUE :
> mystring <- 'abc[de' > gsub("[", "", mystring, fixed=TRUE) [1] "abcde"
Or, if the first in square brackets are square brackets (unescaped), then they are taken as an alphabetic character, and do not have the usual special meaning:
> mystring <- 'a,bc[d]e$' > gsub("[][,$]", "", mystring) [1] "abcde"
source share