Remove Matched Patterns from a String

  • str_remove() removes the first match in each string element
  • str_remove_all() removes all matches in each string element

str_remove() removes the first matched component in each input vector element. It is equivalent to str_replace() with replacement by an empty quote.

library(stringr)
phones <- c( "Mia Smith, 728*971*9652", "Max Lee, 683*976*9876", "Ava Johnson, 912*254*3387")
str_remove(phones, pattern = "\\*")

Output:

[1] "Mia Smith, 728971*9652" "Max Lee, 683976*9876" "Ava Johnson, 912254*3387"

str_remove_all() removes all the matches from each string element.

str_remove_all(phones, pattern = "\\*")

Output:

[1] "Mia Smith, 7289719652" "Max Lee, 6839769876" "Ava Johnson, 9122543387"