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

how to extract a string between two strings in R

If i have a string column in a dataframe :

     a=c ("yhj-22k_loki_res","jh_22k_lok_res","fgdjh_22k_lk_res","jhkkl_kkff_22k_lo_res","j_22k_mm_res","ko-jh_22k_lm_res")

I want to extract whatever between 22k_ and _res

         "loki","lok","lk","lo","mm","lm"

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 a regex lookaround to extract characters (.*) that succeed after the substring "22k_" and precedes "_res"

library(stringr)
str_extract(a, "(?<=22k_).*(?=_res)")
[1] "loki" "lok"  "lk"   "lo"   "mm"   "lm"  
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