[R] regexp inside and outside brackets
Adrian Dușa
dusa.adrian at unibuc.ro
Fri Dec 11 21:55:06 CET 2015
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 your solution helps to trap this error and break the function.
It is of course a different problem than my initial email, but it just
struck me that such an error should ideally be trapped.
Many thanks for the useful insight,
Adrian
On Fri, Dec 11, 2015 at 5:29 PM, Marc Schwartz <marc_schwartz at me.com> wrote:
> 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 Dec 11, 2015, at 9:12 AM, Adrian Dușa <dusa.adrian at unibuc.ro> wrote:
> >
> > 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 <
> jdnewmil at dcn.davis.ca.us>
> > wrote:
> >
> >> 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.
> >>
> >> --
> >> Sent from my phone. Please excuse my brevity.
> >>
> >> On December 11, 2015 5:50:28 AM PST, "Adrian Dușa" <
> dusa.adrian at unibuc.ro>
> >> 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:
> >>> "A1{0}~B0{1} CO{a2}NN{12}"
> >>>
> >>> Everything outside the brackets would be:
> >>>
> >>> "A1 ~B0 CO NN"
> >>>
> >>> and everything inside the brackets would be:
> >>>
> >>> "0 1 a2 12"
> >>>
> >>> I have a working solution involving strsplit(), but I wonder if there
> is a
> >>> more direct way.
> >>> Thanks in advance for any hint,
> >>> Adrian
> >>>
> >>>
> >
> >
> > --
> > Adrian Dusa
> > University of Bucharest
> > Romanian Social Data Archive
> > Soseaua Panduri nr.90
> > 050663 Bucharest sector 5
> > Romania
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
>
--
Adrian Dusa
University of Bucharest
Romanian Social Data Archive
Soseaua Panduri nr.90
050663 Bucharest sector 5
Romania
[[alternative HTML version deleted]]
More information about the R-help
mailing list