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]","BC","{AB}C")
get_enclosed<-function(x,left=c("(","[","<","{"),right=c(")","]",">","}")) {
newx<-rep("",length(x))
for(li in 1:length(left)) {
for(
strcapture() can help here.
> mystrings<-c("ABC","A(B)C","AB(C)")
> strcapture("^[^{]*(\\([^(]*\\)).*$", mystrings,
proto=data.frame(InParen=""))
InParen
1
2 (B)
3 (C)
Classic regular expressions don't do so well with nested parentheses.
Perhaps a perl-style RE could do that.
> strc
On Wed, 12 Jun 2019 15:45:04 +1000
nevil amos wrote:
> # my desired desired output:
> #[1] "" "(B)" "(C)"
(function(s) regmatches(
s,
gregexpr('\\([^)]+\\)', s)
))(c("ABC","A(B)C","AB(C)"))
# [[1]]
# character(0)
#
# [[2]]
# [1] "(B)"
#
# [[3]]
# [1] "(C)"
This matches a
Hi Nevil,
Here's one way to do it. (No doubt some regular-expression-gurus will have
more concise ways to get the job done.)
a1 <- sub(".*\\(","\\(",mystrings)
a2 <- sub("\\).*","\\)",a1)
a2[grep("\\(",a2,invert=TRUE)] <- ""
a2
HTH,
Eric
On Wed, Jun 12, 2019 at 8:46 AM nevil amos wrote:
> H
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("\\(|\\)", my
5 matches
Mail list logo