Regex remove 1-2 character sequences: hyphens literal, not word boundaries I need a regex in R to exclude 1 or 2-character words, but which does not treat hyphens as word boundaries.  Here is an example:

z &lt;- c("regulatory protein SR-B1 na na na na", "Dr Foo is na Editor-in-chief", "na P3 protein is popular na na")  


The intended 'gsub' would produce the result 


  [1] "regulatory protein SR-B1" " Foo Editor-in-chief"     "P3 protein  popular"


This is not produced from the gsub statements below. Instead:  

A - 2-character segments unintentionally dropped from hyphenated words (and hyphens kept).  

B - 2-character segments unintentionally dropped within hyphenated words (hyphens also dropped):  

C - Produces: Error: '-' is an unrecognized escape in character string starting ""
*\b[[-"  

D - Nothing dropped or changed:  

gsub(" *\\b[[:alpha:]]{1,2}\\b *", " ", z) # A

gsub(" *\\b[-[:alpha:]]{1,2}\\b *", " ", z)  # B

gsub(" *\\b[[\-][:alpha:]]{1,2}\\b *", " ", z)  # C

gsub(" *\\b[[\\-][:alpha:]]{1,2}\\b *", " ", z) # D  


Input would be much appreciated.
Try this

gsub(" *\\b(?&lt;!-)\\w{1,2}(?!-)\\b *", " ", z, perl=T)