[R] regular expression question
MacQueen, Don
macqueen1 at llnl.gov
Wed Jan 14 17:03:59 CET 2015
I know you already have a couple of solutions, but I would like to mention
that it can be done in two steps with very simple regular expressions. I
would have done:
s <- c("lngimbintrhofixed","lngimbnointnorhofixed","test",
'rhofixedtest','norhofixedtest')
res <- gsub('norhofixed$', '',s)
res <- gsub('rhofixed$', '',res)
res
[1] "lngimbint" "lngimbnoint" "test"
"rhofixedtest" "norhofixedtest"
(this is for those of us who don't understand regular expressions very
well!)
-Don
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
On 1/12/15, 11:37 AM, "Mark Leeds" <markleeds2 at gmail.com> wrote:
>Hi All: I have a regular expression problem. If a character string ends
>with "rhofixed" or "norhofixed", I want that part of the string to be
>removed. If it doesn't end with either of those two endings, then the
>result should be the same as the original. Below doesn't work for the
>second case. I know why but not how to fix it. I lookrd st friedl's book
>and I bet it's in there somewhere but I can't find it. Thanks.
>
>s <- c("lngimbintrhofixed","lngimbnointnorhofixed","test")
>
>result <- sub("^(.*)([n.*|r.*].*)$","\\1",s)
>
> print(result)
>[1] "lngimbint" "lngimbnointno" "test"
>
> [[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.
More information about the R-help
mailing list