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

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

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.

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 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

>Solution :

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