Home > Software engineering >  Move leading numbers after last character using regular expreession
Move leading numbers after last character using regular expreession

Time:05-29

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:

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"

CodePudding user response:

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"
  • Related