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,
> 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:
>
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.
--
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
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 <-
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
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
7 matches
Mail list logo