-2

I have the following string: x = '(example)' and I want to remove both parentheses, in one command, using stringr.

I tried str_remove(x, '\(|\)'), but this removed only one parenthese (the first one). Any idea why this did not work and how it can work?

There are several posts on similar issues, but no one helped: https://stringr.tidyverse.org/articles/regular-expressions.html or remove parenthesis from string or stringr: Removing Parentheses and Brackets from string

Thanks

Rtist
  • 3,825
  • 2
  • 31
  • 40

2 Answers2

3

We can use str_remove_all instead of str_remove as this matches only the first instance

library(stringr)
str_remove_all(x, "[()]")
#[1] "example"
akrun
  • 874,273
  • 37
  • 540
  • 662
0

You can try to replace "(" and ")" with "".

gsub("[()]" , "" , "(example)")
#[1] "example"
Hsiang Yun Chan
  • 141
  • 2
  • 4