how to use stringr:str_remove to remove the pattern of multiple occurence

I have the string
x <- c("The first apple is good, and the second and third apples are good too")
I want the string y to be "The first , and the second and third too".

I tried the code y <- str_remove_all(x, "apple.+good"), but it yield
"The first too"

Could someone help me to solve this question?

The ? after the + makes it "non-greedy", matching the minimum length.

library(stringr)
x <- c("The first apple is good, and the second and third apples are good too")
y <- str_remove_all(x, "apple.+?good")
y
#> [1] "The first , and the second and third  too"

Created on 2022-05-04 by the reprex package (v2.0.1)

1 Like

Exactly! Thank you very much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.