[R] reg expr that retains only bracketed text from strings

William Dunlap wdun|@p @end|ng |rom t|bco@com
Wed Jun 12 16:32:14 CEST 2019


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]]



More information about the R-help mailing list