Home > OS >  Remove all characters in a string after the last ocurrence of a pattern in R
Remove all characters in a string after the last ocurrence of a pattern in R

Time:05-10

I want to remove all the characters after the last ocurrence of a specific pattern in a string, in R.

For example:

string = "asdsads dfdsfd>x 442 /<sdasvre (geqwe) ge ge ge regthyty "

I would like to remove everything after the last ocurrence of the pattern "ge" and end up with:

"asdsads dfdsfd>x 442 /<sdasvre (geqwe) ge ge ge".

CodePudding user response:

You can use a capture group to capture all strings before the last "ge" (^(.*ge)), and replace that whole thing with that capture group (\\1).

sub('^(.*ge). $', '\\1', string)
[1] "asdsads dfdsfd>x 442 /<sdasvre (geqwe) ge ge ge"

CodePudding user response:

You could use a negative lookahead here:

string <- "asdsads dfdsfd>x 442 /<sdasvre (geqwe) ge ge ge regthyty "
output <- sub("\\bge (?!.*\\bge\\b).*", "ge", string, perl=TRUE)
output

[1] "asdsads dfdsfd>x 442 /<sdasvre (geqwe) ge ge ge"
  • Related