[R] regular expression question

Mark Leeds markleeds2 at gmail.com
Mon Jan 12 20:37:41 CET 2015


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]]



More information about the R-help mailing list