str1 <- "0992006 09:00" gsub("(?<=^| )0+", "", str1, perl=TRUE) #[1] "992006 9:00"
For situations like the following, this could be:
str2 <- "0992006 00:00" gsub("(?<=^| )0", "", str2, perl=TRUE) #[1] "992006 0:00"
Explanation
The idea here is to use look behind (?<=^| )0+ to match 0 s
if this happens either at the beginning of the line (?<=^
or |
if it follows the space )0+
and replace those that match 0s with "" in the second part of the gsub argument.
In the second line, hour and minutes all 0's . Thus, using the first code will result in:
gsub("(?<=^| )0+", "", str2, perl=TRUE) #[1] "992006 :00"
It is not clear what the OP will take as a result. So I thought that instead of deleting just 0 before : it would be better if only 0 remained. So, I replaced the code 0+ with one 0 and replaced it with "" .
akrun
source share