Extracting Words of specific length in R using regular expressions

r, regex, string

Solution

  gsub("\\b[a-zA-Z0-9]{4,10}\\b", "", m) 
 "! # is gr8. I  likewhatishappening ! The  of   is ! the aforementioned  is ! #Wow"

Let's explain the regular expression terms :

- \b matches at a position that is called a "word boundary". This match is zero-length.

- [a-zA-Z0-9] :alphanumeric

- {4,10} :{min,max}

if you want to get the negation of this so , you put it between() and you take //1

gsub("([\\b[a-zA-Z0-9]{4,10}\\b])", "//1", m) 

"Hello! #London is gr8. I really likewhatishappening here! The alcomb of Mount Everest is excellent! the aforementioned place is amazing! #Wow"

It is funny to see that words with 4 letters exist in the 2 regexpr.

Problem

I have a code like (I got it here): ``` m<- c("Hello! #London is gr8. I really likewhatishappening here! The alcomb of Mount Everest is excellent! the aforementioned place is amazing! #Wow") x<- gsub("\\<[a-z]\\{4,10\\}\\>","",m) x ``` I tried other ways of doing it, like ``` m<- c("Hello! #London is gr8. I really likewhatishappening here! The alcomb of Mount Everest is excellent! the aforementioned place is amazing! #Wow") x<- gsub("[^(\\b.{4,10}\\b)]","",m) x ``` I need to remove words which are lesser than 4 or greater than 10 in length. Where am I going wrong?

Original source