str_replace_all on "[" character

Hey everyone

I am fiddling around with the stringr package and want to extract two values from a string into separate columns. My idea was first to replace the brackets by empty characters, then I have a comma-separated list that I can split. But I cannot declare the bracket as a character to be replaced.

tmp <- "[10, 20]"
str_replace_all(tmp, "[]", "")

This gives me an error that the brackets belong to the bracket expression. I tried a lot like "["[""]"]" and using \ as escape character but nothing worked.

I guess the solution is quite easy, but I cannot find it right now.

Thanks for helping!

Here's a way to replace the brackets with empty characters.

library(stringr)
tmp <- "[10, 20]"
str_replace_all(tmp, "\\[|\\]", "")
#> [1] "10, 20"

Created on 2022-12-21 with reprex v2.0.2.9000

1 Like

thank you, good Sir.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.