-2

I am having difficulty achieving the following with stringr: I want to remove stray characters (up to two) flanked by a space on either side. I cannot get stringr to give me good results without an error. Two examples are below. Thank you in advance.

string1<-'john a smith'
output1<-'john smith'
string2<-'betty ao smith'
output2<-'betty smith'
r2evans
  • 141,215
  • 6
  • 77
  • 149

1 Answers1

1
gsub(" \\S{1,2} ", " ", c('john a smith', 'betty ao smith'))
# [1] "john smith"  "betty smith"
  • \\S is a non-space character
  • {.} allows for repeats of the preceding pattern; for instance
    • {2,} at least 2
    • {,3} no more than 3
    • {1,2} between 1 and 2

Same in stringr, since it's "just regex" :-)

stringr::str_replace(c('john a smith', 'betty ao smith'), " \\S{1,2} ", " ")
r2evans
  • 141,215
  • 6
  • 77
  • 149