Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate a backreference from a number

Under R, I'm trying to separate letters and numbers in a string in order to add a 0 when the number is lower than 10 (one digit).

Example :

my_strings=c("GIR1", "GIR20", "GIR3ABC")

At the end, I want my strings = "GIR01", "GIR20", "GIR03ABC"

So I tried :

result=gsub("([A-Z]*)([1-9]{1})([A-Z]*)", "\\1Z\\2\\3",my_strings)

but I have 2 problems :

The first : it doesn't select strings with only one digit (it selects all), even though I specified {1} and the second problem is that instead of the Z that I wrote in the replacement string, I want a 0, but then, it becomes \\10. Does anyone knows how to separate the 1 and the 0 ?

Thank You

like image 470
Georges Legall Avatar asked Dec 12 '25 16:12

Georges Legall


2 Answers

gsubfn is like gsub except the replacement string can be a replacement function which inputs the match replacing it with the output of the function. The function can be specified in the ordinary way or in formula notation as we use here. As a consequence this one-liner can use a simple regex.

library (gsubfn)

gsubfn("\\d+", ~ if (nchar(x) == 1) paste0(0, x) else x, my_strings)
## [1] "GIR01"    "GIR20"    "GIR03ABC"

This also works

gsubfn("\\d+", ~ sprintf("%02d", as.numeric(x)), my_strings)
## [1] "GIR01"    "GIR20"    "GIR03ABC"

Note

The input from the question

my_strings <- c("GIR1", "GIR20", "GIR3ABC")
like image 169
G. Grothendieck Avatar answered Dec 14 '25 08:12

G. Grothendieck


We can use gregexpr to identify the numbers, and regmatches to extract and reassign back into the strings.

my_strings <- c("GIR1", "GIR20", "GIR3ABC")
gre <- gregexpr("\\d+", my_strings)

regmatches(my_strings, gre)
# [[1]]
# [1] "1"
# [[2]]
# [1] "20"
# [[3]]
# [1] "3"
regmatches(my_strings, gre) <- sprintf("%02d", as.integer(unlist(regmatches(my_strings, gre))))
my_strings
# [1] "GIR01"    "GIR20"    "GIR03ABC"
like image 34
r2evans Avatar answered Dec 14 '25 07:12

r2evans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!