Home > OS >  Removal of specific string and anything after
Removal of specific string and anything after

Time:06-25

I would like to remove 'sat' and anything after but not 'saturday'. Although this seems quite simple I have been unable to find a thread on this.

Example:

text <- c("good morning amer sat","this morning saturday")

Desired Result:

"good morning amer","this morning saturday"

Everything I do removes saturday.

CodePudding user response:

We could use word boundary (\\b)

sub("\\s*\\bsat\\b", "", text)

-output

[1] "good morning amer"     "this morning saturday"

Or with stringr

library(stringr)
str_remove(text, "\\s*\\bsat\\b")
[1] "good morning amer"     "this morning saturday"

CodePudding user response:

Here with str_replace:

library(stringr)

str_replace(text, ' sat$', '')

[1] "good morning amer"    
[2] "this morning saturday"

CodePudding user response:

Another possible solution, using negative lookaround and stringr::str_remove (although, I am not sure whether you want to remove sat only or remove sat and every following character):

library(stringr)

str_remove(text, "\\s*sat(?!urday).*$")

#> [1] "good morning amer"     "this morning saturday"
  • Related