Re: [R] regexp inside and outside brackets

2015-12-11 Thread phgrosjean
It depends the complexity of your expression. If you are sure you don’t have nested brackets, and pairs of brackets always match, this will take everything outside the brackets: str <- "A1{0}~B0{1} CO{a2}NN{12}” gsub("\\{[^}]*\\}", " ", str) Philippe Grosjean > On 11 Dec 2015, at 14:50,

Re: [R] regexp inside and outside brackets

2015-12-11 Thread Marc Schwartz
> On Dec 11, 2015, at 7:50 AM, Adrian Dușa wrote: > > For the regexp aficionados, out there: > > I need a regular expression to extract either everything within some > brackets, or everything outside the brackets, in a string. > > This would be the test string: >

Re: [R] regexp inside and outside brackets

2015-12-11 Thread Jeff Newmiller
The gsub function is your friend. s <- "A1{0}~B0{1} CO{a2}NN{12}" gsub( "([^{}]*)\\{([^{}]*)\\}", "\\1 ", s ) gsub( "([^{}]*)\\{([^{}]*)\\}", "\\2 ", s ) but keep in mind that there are many resources on the Internet for learning about regular expressions... they are hardly R-specific. --

Re: [R] regexp inside and outside brackets

2015-12-11 Thread Marc Schwartz
Hi, Needless to say, Jeff's solution is easier than my second one. I was wrestling in dealing with the greedy nature of regex's and so shifted to thinking about the use of the functions that I proposed in the second scenario. Also, I was a bit hypo-caffeinated ... ;-) Regards, Marc > On

Re: [R] regexp inside and outside brackets

2015-12-11 Thread Adrian Dușa
Thanks very much, Marc and Jeff. Jeff's solutions seem to be simple one liners. I really need to learn these things, too powerful to ignore. Thank you very much, Adrian On Fri, Dec 11, 2015 at 5:05 PM, Jeff Newmiller wrote: > The gsub function is your friend. > > s <-

[R] regexp inside and outside brackets

2015-12-11 Thread Adrian Dușa
For the regexp aficionados, out there: I need a regular expression to extract either everything within some brackets, or everything outside the brackets, in a string. This would be the test string: "A1{0}~B0{1} CO{a2}NN{12}" Everything outside the brackets would be: "A1 ~B0 CO NN" and

Re: [R] regexp inside and outside brackets

2015-12-11 Thread Adrian Dușa
Actually, Marc, I think your solution might be more useful than it first seemed. The correct usage of a string would be for someone to provide complete pairs of outside and inside brackets information, like: A{1} B{0} But if a user doesn't provide this "standard" notation, as in: A{1} B then