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

Using gsub to extract the a code (numbers and characters)

I have a string and I wish to extract the code but I’m not quite able to re-create it, the code will change each time but the IDs part will always be the same

mycode
"IDs/5362E-C3A~16DFR-54RFFG"

What I’m looking to get;

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

mycode
"5362E-C3A"

My attempt, I tried to break it down into 3 parts, Ids, the code I need and the tilde and other waste bit of code then saying I want to keep the first instance inside of the brackets.

mycode = gsub("IDs/([A-9]+)~[A-9+]", "\\1", mycode) 

>Solution :

Assuming you want everything in between the / and the ~, you could try the following regex replacement which uses an alternation:

x <- "IDs/5362E-C3A~16DFR-54RFFG"
output <- gsub(".*/|~.*", "", x)
output

[1] "5362E-C3A"
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