Eric Green
Eric Green

Reputation: 7735

replacing text with a conditional pattern

I am importing a file with column names that take the following format:

x <- "text.text.text.text.name"

In most cases, the following line is good for removing everything before the final period, leaving only the variable name I want (name):

gsub(".*\\.","", x)

However, some variables have the following format:

y <- "text.text.text.text.name.R1"

I'd like to keep name.R1 when the final period is followed by a capital "R". How would I modify the gsub argument to permit this change?

Upvotes: 0

Views: 263

Answers (3)

Eric Green
Eric Green

Reputation: 7735

@kasterma's solution worked best for me:

gsub("(.*\\.)([^R])","\\2", x)

Upvotes: 0

Shenglin Chen
Shenglin Chen

Reputation: 4554

gsub('.*\\.(\\w+.\\w+$)','\\1',y)
#[1] "name.R1"

Upvotes: 0

akuiper
akuiper

Reputation: 215117

You can use a negative look ahead to constrain the final period:

gsub(".*\\.(?!R)", "", x, perl = T)
# [1] "name"

gsub(".*\\.(?!R)", "", y, perl = T)
# [1] "name.R1"

Upvotes: 1

Related Questions