2

This is addressed in this question and answered here (and this question is also cited), but none of them actually answer the question -- the answer provided to the first question sidesteps the issue. The more general regex answer doesn't appear to work in R:

gsub('[\\]`]', '', '`]')
# [1] "`]"

I'm trying to match a specific set of punctuation so [:punct:] is too general.

Community
  • 1
  • 1
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198

2 Answers2

2

The right bracket inside the character class appear to be work for me unescaped, i.e. just use this:

gsub('[]`]', '', '`]')
[1] ""

It appears that the R interpreter "knows" that a right or left bracket inside a balanced character class (i.e. consisting of a proper left bracket and right bracket) is actually just a character.

Here is proof of this claim:

gsub('[]a-z`]', '', '`]abc')
[1] ""

If the first [] were a closed and empty character class, then the range a-z should not have worked, and the letters b and c should have remained untouched.

Also: If we try to substitute using an empty character class, we get an error, e.g.

gsub('[]', '', '`]')
Error in gsub("[]", "", "`]") : 
  invalid regular expression '[]', reason 'Missing ']''
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • no, the character class is then just `[]`, and then it matches `\`]` directly, so this won't work on the string `\` ]`, for example – MichaelChirico May 08 '17 at 05:00
  • 1
    @hwnd Sorry, I didn't read the link and also you aren't Akrun, so I wasn't particularly scared either :-) If this is a duplicate you should close it then. – Tim Biegeleisen May 08 '17 at 05:14
0

Just escape the bracket and other special chars, e.g.,

gsub("\\]\\`", "", "]`")
mkearney
  • 1,266
  • 10
  • 18