# removing part of a string

4 messages
Open this post in threaded view
|

## removing part of a string

 dear all, I am stuck on the following problem. Give a string like ss1<- "z:f(5, a=3, b=4, c='1:4', d=2)" or ss2<- "f(5, a=3, b=4, c=\"1:4\", d=2)*z" I would like to remove all entries within parentheses.. Namely, I aim to obtain respectively "z:f()" or "f()*z" I played with sub() and gsub() but without success.. Thank you very much for your time, best, vito -- ============================================== Vito M.R. Muggeo Dip.to Sc Econom, Az e Statistiche Università di Palermo viale delle Scienze, edificio 13 90128 Palermo - ITALY tel: 091 23895240 fax: 091 485726 http://dssm.unipa.it/vmuggeoAssociate Editor, Statistical Modelling Chair, Statistical Modelling Society ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: removing part of a string

 Hello, Try this. ss1 <- "z:f(5, a=3, b=4, c='1:4', d=2)" ss2 <- "f(5, a=3, b=4, c=\"1:4\", d=2)*z" fun <- function(s) sub("(\\().*(\\))", "\\1\\2", s) fun(ss1) #[1] "z:f()" fun(ss2) #[1] "f()*z" Hope this helps, Rui Barradas On 5/21/2018 2:33 PM, Vito M. R. Muggeo wrote: > dear all, > I am stuck on the following problem. Give a string like > > ss1<- "z:f(5, a=3, b=4, c='1:4', d=2)" > > or > > ss2<- "f(5, a=3, b=4, c=\"1:4\", d=2)*z" > > I would like to remove all entries within parentheses.. Namely, I aim to > obtain respectively > > "z:f()" or "f()*z" > > I played with sub() and gsub() but without success.. > Thank you very much for your time, > > best, > vito > > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.