0

I know that this question looks identical to the one posted here, but I still can't get my expression to run. I'm trying to locate "Mr." and using the regular expression "Mr\." to do so. Here is the full line of code:

HPText$text<-str_replace(string = HPText$text, pattern= "Mr\\.", replacement 
= "Mister")

Can't wait to find out what stupid mistake I'm making.

phiver
  • 23,048
  • 14
  • 44
  • 56
Tanner Phillips
  • 392
  • 1
  • 11
  • What is the result you expect? Your `str_replace` does replace a single `Mr.` with `Mister`. Why use a regex here, BTW? You may use a fixed search for that. If you need to replace all occurrences of `Mr.` with `Mister`, use `str_replace_all(HPText$text, fixed("Mr."), "Mister")`. – Wiktor Stribiżew Oct 27 '18 at 15:56
  • `str_replace(string = c("Mr. Jones", "Jones, Mr."), pattern= "Mr\\.", replacement = "Mister")` works fine. Please create a reproducible example with input, expected output and where in your case it doesn't return what you expect. – phiver Oct 27 '18 at 15:58
  • strng<-"In an instant he had pull Mr. Weasley and Mr. Malfoy apart. strng<-str_replace(string = strng, pattern= "(Mr\\.)", replacement = "Mister") > strng [1] "In an instant he had pull Mister Weasley and Mr. Malfoy apart." Sorry, terrible formatting. It's working inconsistently. I'm at a loss. @phiver – Tanner Phillips Oct 27 '18 at 16:03
  • 1
    You just need to replace all occurrences, `str_replace_all(HPText$text, fixed("Mr."), "Mister")` as I mention in my top comment. – Wiktor Stribiżew Oct 27 '18 at 16:04
  • Got it, thank you! – Tanner Phillips Oct 27 '18 at 16:06

0 Answers0