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

nevil amos nev||@@mo@ @end|ng |rom gm@||@com
Wed Jun 12 07:45:04 CEST 2019


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



More information about the R-help mailing list