I am trying to catch all the words in a longer string which contain between 9 and 12 characters. I am using str_extract from the stringr package using regex. The ‘words’ can contain both letters and numbers. So I thought using either \wor [:alnum:] in combination with a range [9-12]. Below I have an example of what I want, and what I’ve tried
str_ex <- "Hello these are the words i want 01234567acht and 1234h5678 but also a01b2c34t2f and delete the rest"
str_extract_all(str_ex,"\\w{9-12}+")
str_extract_all(str_ex,"\\w[9-12]+")
str_extract_all(str_ex,"[:alnum:]{9-12}+")
what I want is to extract the longer ‘words’
> "01234567acht", "1234h5678", "a01b2c34t2f"
I have tried all sorts of variations on the given examples, but I can’t find the one that works. I am sure it is easy for someone here to answer, however it is hard for me to find online.
Any help will be appreciated, kind regards
>Solution :
Use comma rather than minus and omit the +
str_extract_all(str_ex,"\\w{9,12}")
giving:
[[1]]
[1] "01234567acht" "1234h5678" "a01b2c34t2f"