[R] regular expression question

Gabor Grothendieck ggrothendieck at gmail.com
Tue Mar 3 13:35:42 CET 2009


Here are two solutions using gsubfn package.
strapply works by matching the what you want
rather than what you don't want which may make
it easier in this case.  The two solutions are the
same except we use \\ escapes in the first and
[ ... ] in the second, i.e. \\( has the same effect
as [(].   In each case we first match the ( then
a sequence of characters that is not ] and finally
we match the terminating ].

> library(gsubfn)
> x <- "(-0.791,-0.263].(-38,-1.24].(0.96,2.43]"

> strapply(x, "\\([^]]+[]]")[[1]]
[1] "(-0.791,-0.263]" "(-38,-1.24]"     "(0.96,2.43]"

> strapply(x, "[(][^]]+[]]")[[1]]
[1] "(-0.791,-0.263]" "(-38,-1.24]"     "(0.96,2.43]"


On Tue, Mar 3, 2009 at 1:16 AM,  <markleeds at verizon.net> wrote:
> can someone show me how to use a regular expression to break the string at
> the bottom up into its three components :
>
> (-0.791,-0.263]
> (-38,-1.24]
> (0.96,2.43]
>
> I tried to use strplit because of my regexpitis ( it's not curable. i've
> been to many doctors all over NYC. they tell me there's no cure  )  but it
> doesn't work because there also dots inside  the brackets. Thanks.
>
> (-0.791,-0.263].(-38,-1.24].(0.96,2.43]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>




More information about the R-help mailing list