fork download
  1. trial.string<-c("Hat","coif","hatter","HATS","plushy","no hat","what","hat no","hats","HAT, what","what, hat, no, hats","A water hat")
  2. stringr::str_replace_all(trial.string,
  3. "(?i)(?<!\\b(?:no|what)\\b.{0,1000})\\bhats?(?!.*\\b(?:no|what)\\b)", " ")
Success #stdin #stdout 0.28s 42532KB
stdin
Standard input is empty
stdout
 [1] " "                   "coif"                " ter"               
 [4] " "                   "plushy"              "no hat"             
 [7] "what"                "hat no"              " "                  
[10] "HAT, what"           "what, hat, no, hats" "A water  "