'Remove elements in a string after an "exact match" in stringr

I have a character like this:

a <- "PP Express Transaction_ID: 1234"

I would like to remove everything after "PP Express " like:

a_final <- "PP Express"

I tried with:

a <- "PP Express Transaction_ID: 1234"
gsub("PP Express*", "", a)

but I get the exact contrary...

moreover: id I wanted to apply this on a dataframe column, I would like to perform the operation only for the rows containing this exact substring!

I tried with str_extract() but I get null values if the row doesn't contain the substring at all.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source