-1

I'm trying to use gsub to remove certain parts of a string. However, I can't get it to work, and I think it's because the string to be removed contains brackets. Is there any way around this? Thanks for any help.

The command I want to use:

gsub('(4:4aCO)_','', '(5:3)_(4:4)_(5:3)_(4:4)_(4:4aCO)_(6:2)_(4:4a)')

Returns:

#"(5:3)_(4:4)_(5:3)_(4:4)_(4:4aCO)_(6:2)_(4:4a)"

Expected output:

#"(5:3)_(4:4)_(5:3)_(4:4)_(6:2)_(4:4a)"

A quick test to see if brackets were the problem:

gsub('te','', 'test')
#[1] "st"

gsub('(te)','', '(te)st')
#[1] "()st"
Margins
  • 89
  • 8
  • 2
    Do you need `gsub('\\(4:4aCO\\)','', '(5:3)(4:4)(5:3)(4:4)(4:4aCO)(6:2)_(4:4a)')` ? – Ronak Shah Feb 02 '17 at 13:29
  • 1
    You are correct. Parenthesis have special meanings in regexs. You can either escape them in your regex or use fixed=TRUE as akrun suggests. – Dason Feb 02 '17 at 13:38

1 Answers1

4

We can by placing the brackets inside the square brackets as () is a metacharacter

gsub('[(]4:4aCO[)]','', '(5:3)(4:4)(5:3)(4:4)(4:4aCO)(6:2)_(4:4a)')

Or with fixed = TRUE to evaluate the literal meaning of that character

gsub('(4:4aCO)','', '(5:3)(4:4)(5:3)(4:4)(4:4aCO)(6:2)_(4:4a)', fixed = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662