[R] reg expr that retains only bracketed text from strings
Jim Lemon
drj|m|emon @end|ng |rom gm@||@com
Thu Jun 13 01:09:12 CEST 2019
Hi Nevil,
In case you are still having trouble with this, I wrote something in R
that should do what you want:
mystrings<-c("ABC","A(B)C","AB[C]","<A>BC","{AB}C")
get_enclosed<-function(x,left=c("(","[","<","{"),right=c(")","]",">","}")) {
newx<-rep("",length(x))
for(li in 1:length(left)) {
for(xi in 1:length(x)) {
lp<-regexpr(left[li],x[xi],fixed=TRUE)
rp<-regexpr(right[li],x[xi],fixed=TRUE)
if(lp > 0 && rp > 0)
newx[xi]<-substr(x[xi],lp+1,rp-1)
}
}
return(newx)
}
get_enclosed(mystrings)
Jim
On Thu, Jun 13, 2019 at 12:32 AM William Dunlap via R-help
<r-help using r-project.org> wrote:
>
> strcapture() can help here.
>
> > mystrings<-c("ABC","A(B)C","AB(C)")
> > strcapture("^[^{]*(\\([^(]*\\)).*$", mystrings,
> proto=data.frame(InParen=""))
> InParen
> 1 <NA>
> 2 (B)
> 3 (C)
>
> Classic regular expressions don't do so well with nested parentheses.
> Perhaps a perl-style RE could do that.
> > strcapture("^[^{]*(\\([^(]*\\)).*$", proto=data.frame(InParen=""),
> x=c("()", "a(s(d)f)g"))
> InParen
> 1 ()
> 2 (d)f)
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
> On Tue, Jun 11, 2019 at 10:46 PM nevil amos <nevil.amos using gmail.com> wrote:
>
> > Hi
> >
> > I am trying to extract only the text contained in brackets from a vector of
> > strings
> > not all of the strings contain closed bracketed text, they should return an
> > empty string or NA
> >
> > this is what I have at the moment
> >
> >
> > mystrings<-c("ABC","A(B)C","AB(C)")
> >
> > substring(mystrings, regexpr("\\(|\\)", mystrings))
> >
> >
> > #this returns the whole string if there are no brackets.
> > [1] "ABC" "(B)C" "(C)"
> >
> >
> > # my desired desired output:
> > # [1] "" "(B)" "(C)"
> >
> > many thanks for any suggestions
> > Nevil Amos
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
More information about the R-help
mailing list