3

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.

megmac
  • 547
  • 2
  • 11

3 Answers3

3

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"
akrun
  • 874,273
  • 37
  • 540
  • 662
3

Here with str_replace:

library(stringr)

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

[1] "good morning amer"    
[2] "this morning saturday"
TarJae
  • 72,363
  • 6
  • 19
  • 66
1

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"
PaulS
  • 21,159
  • 2
  • 9
  • 26