Replacing a character inside a substring Inside parentheses in R

I am trying to replace the commas inside all sets of parentheses with a semicolon, but not change any commas outside the parentheses.

So for example:

"a, b, c (1, 2, 3), d, e (4, 5)" 

should become:

 "a, b, c (1; 2; 3), d, e (4; 5)" 

I started doing this with gsub, but it is very difficult for me to understand / figure out how to identify these commas in parentheses.

I would call myself an advanced beginner with R, but with regular expressions and text manipulation, complete noob. Any help you can provide would be great.

+5
source share
2 answers

you can use

 a <- "a, b, c (1, 2, 3), d, e (4, 5)" gsub(",(?=[^()]*\\))", ";", a, perl=T) ## [1] "a, b, c (1; 2; 3), d, e (4; 5)" 

Watch the IDEONE demo

Regular match ...

  • , - a comma if ...
  • (?=[^()]*\\)) - it is followed by 0 or more characters, except ( or ) (with [^()]* ) and the letter ) .
+6
source
 gsub("(?<=\\d),", ";", string, perl=T) 
0
source

All Articles