I am close but can’t seem to get this to work. I’d like to achieve converting this string “1960-12-031 22:00:010” to this string “1960-12-31 22:00:10” by finding the places where two numbers are preceded by a 0 and then stripping the 0.
I’ve got the regex working:
txt <- "1960-12-031 22:00:010"
gsub("(0+[0-9]{2})", "\\1", txt, perl=TRUE)
I just can’t seem to figure out what to do with the “\\1” to strip out the first character.
Any assistance would be appreciated –
Note that this will replace 031, 0031, 00031, etc. with 31. If you want to remove only the first zero, use
"0{1}([0-9]{2})".Update: As per the recommendation in the comment you can use
"[\\D]0+([0-9]{2})"to avoid matching zeros within numbers such as 2012.