1

I ran this line of code:

str_remove("abc(d)efg", "abc")

and it gave me what I wanted:

[1] "(d)efg"

However when I ran:

str_remove("abc(d)efg", "abc(d)")

I hoped I would see

[1] "efg"

but instead I saw

[1] "abc(d)efg"

Are the ( and the ) treated somehow as special characters inside the str_remove() call?

Thank you.

Alan
  • 619
  • 6
  • 19

1 Answers1

3

Use fixed as wrappers as the () are metacharacters and by default it is in regex mode i.e. () will assume that we are capturing characters within

library(stringr)
str_remove("abc(d)efg", fixed("abc(d)"))
[1] "efg"
akrun
  • 874,273
  • 37
  • 540
  • 662