Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find all words in string containing number of characters in range R

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use comma rather than minus and omit the +

str_extract_all(str_ex,"\\w{9,12}")

giving:

[[1]]
[1] "01234567acht" "1234h5678"    "a01b2c34t2f" 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading