I have a matching problem that I cant solve. mystring = "xxx{XX}yy{YYY}zzz{Z}" where "x","X","y","Y","z","Z" basiclly can be anything, letters, digits etc. I'm only interested in the content within each "{}". I am close but not really there yet. library(gsubfn) strapply(mystring,"\\{[^\\}]+",, perl=F) gives me [[1]] [1] "{XX" "{YYY" "{Z" but what should I add in the code to remove the "{" in the answer Regards Tom -- View this message in context: http://www.nabble.com/matching-problem-tp18850718p18850718.html Sent from the R help mailing list archive at Nabble.com.
One option is: strapply(mystring, "\\{[^\\}]+" , function(x)gsub("\\{", "", x), perl = F) On Wed, Aug 6, 2008 at 10:01 AM, Tom.O <tom.olsson at dnbnor.com> wrote:> > I have a matching problem that I cant solve. > mystring = "xxx{XX}yy{YYY}zzz{Z}" where "x","X","y","Y","z","Z" basiclly can > be anything, letters, digits etc. I'm only interested in the content within > each "{}". > > I am close but not really there yet. > > library(gsubfn) > strapply(mystring,"\\{[^\\}]+",, perl=F) > > gives me > [[1]] > [1] "{XX" "{YYY" "{Z" > > but what should I add in the code to remove the "{" in the answer > > Regards Tom > -- > View this message in context: http://www.nabble.com/matching-problem-tp18850718p18850718.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
On Wed, Aug 6, 2008 at 9:01 AM, Tom.O <tom.olsson at dnbnor.com> wrote:> > I have a matching problem that I cant solve. > mystring = "xxx{XX}yy{YYY}zzz{Z}" where "x","X","y","Y","z","Z" basiclly can > be anything, letters, digits etc. I'm only interested in the content within > each "{}". > > I am close but not really there yet. > > library(gsubfn) > strapply(mystring,"\\{[^\\}]+",, perl=F) > > gives me > [[1]] > [1] "{XX" "{YYY" "{Z" > > but what should I add in the code to remove the "{" in the answer >Surround the portion you want with parentheses. That makes it a backreference and you can ask for the backreference rather than the entire match. -1 means return 1 backreference but not the entire match. strapply(mystring,"\\{([^\\}]+)", backref = -1)