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

Move leading numbers after last character using regular expreession

I would like to position all leading numbers after the last character between two underscores in a string:

I have this vector:

have <- c("6ABCD23102019", "3ABCD23102019", "9ABCD23102019", "10ABCD23102019")

[1] "6ABCD23102019" "3ABCD23102019" "9ABCD23102019" "10ABCD23102019"

I would like to get this:

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

desired <- c("ABCD_6_23102019", "ABCD_3_23102019", "ABCD_9_23102019", "ABCD_10_23102019")

[1] "ABCD_6_23102019" "ABCD_3_23102019" "ABCD_9_23102019" "ABCD_10_23102019"

This 6ABCD23102019 should become this ABCD_6_23102019

Background: Within file renaming and copying maneuvers I need to rename the files.

I have tried:

library(stringr)
x <- str_sub(have, 1,1)
helper <- str_replace(have, '\\dA', 'A')
result <- str_replace(helper, 'D', 'D_x_')
result

[1] "ABCD_x_23102019" "ABCD_x_23102019" "ABCD_x_23102019" "ABCD_x_23102019"

>Solution :

Use capture groups to capture the one or more digits at the start (^) followed by the upper case letters and then rearrange the backreferences (\\1, \\2) in reverse order

library(stringr)
str_replace(have, "^(\\d+)([A-Z]+)", "\\2_\\1_")

-output

[1] "ABCD_6_23102019"  "ABCD_3_23102019"  "ABCD_9_23102019"  "ABCD_10_23102019"
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