[R] regular expression question

John McKown john.archie.mckown at gmail.com
Mon Jan 12 20:45:34 CET 2015


No HTML please. it makes me itchy! <grin/>

> s <- c("lngimbintrhofixed","lngimbnointnorhofixed","test")
> sub('(no)?rhofixed$','',s)
[1] "lngimbint"   "lngimbnoint" "test"
>


On Mon, Jan 12, 2015 at 1:37 PM, 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.
>



-- 
​
While a transcendent vocabulary is laudable, one must be eternally careful
so that the calculated objective of communication does not become ensconced
in obscurity.  In other words, eschew obfuscation.

111,111,111 x 111,111,111 = 12,345,678,987,654,321

Maranatha! <><
John McKown

	[[alternative HTML version deleted]]



More information about the R-help mailing list