Home > Blockchain >  Add an underscore before the last digit followed by any number in a string
Add an underscore before the last digit followed by any number in a string

Time:12-31

This must be easy, but I can't handle it. Sorry for that!

I have this string:

string <- c("AB1C1", "AB2C2", "AB3C20")
[1] "AB1C1"  "AB2C2"  "AB3C20"

I would like to ADD an underscore before the last character followed by any digit.

Desired output:

[1] "AB1_C1"  "AB2_C2"  "AB3_C20"

I have tried so far:

I can match with regex: [A-Z][0-9] $ the last character followed by any digit.

But I don't know how to ADD an underscore before this match

CodePudding user response:

You can use

sub("(.*)(\\D\\d )$", "\\1_\\2", string)
## => [1] "AB1_C1"  "AB2_C2"  "AB3_C20"
sub("(\\D\\d )$", "_\\1", string)
## => [1] "AB1_C1"  "AB2_C2"  "AB3_C20"

See the regex demo / regex demo #2. Details:

  • (.*) - Group 1: any zero or more chars as many as possible
  • (\D\d ) - Group 2: any non-digit and then one or more digits
  • $ - end of string.

See the R demo:

string <- c("AB1C1", "AB2C2", "AB3C20")
sub("(.*)(\\D\\d )$", "\\1_\\2", string)
## => [1] "AB1_C1"  "AB2_C2"  "AB3_C20"
  • Related