Stringr - removing leading zero from both sides of hyphen

Viewed 158

Per title, I am looking for a way to remove the leading zero from both sides of the hyphen.

codes <- c("0002-01014", "0020-0014","00014-00010")
want <- c("2-1014","20-14","14-10")

I suppose one can split the numbers into two, remove leading zero, then paste together, but I was wondering if there is more refined method using single step of str_extract(), str_replace(), or similars.

Edit:

I am currently using

str_split("0002-01010","-",simplify=T) %>%
  str_replace("^0+(?!$)", "") %>%
  str_c(collapse="-")

and was curious if there is an alternative method that can be achieved in single line.

2 Answers

You can use

codes <- c("0002-01014", "0020-0014","00014-00010", "00000-122345")
gsub("\\b0+\\B", "", codes, perl=TRUE)
# => [1] "2-1014"   "20-14"    "14-10"    "0-122345"

See the R demo and the regex demo.

The \b matches a position between start of string or a non-word char and a word char, here, at the beginning of the string or between - and the next digit. 0+ matches one or more zeros, as many as possible. \B makes sure the match stops before a digit, so it there are only zeros in the number, the last one remains intact.

You might as well match and remove zeros at the beginning or after a dash, but not at the end or before a dash:

codes <- c("0002-01014", "0020-0014","00014-00010", "00000-000330")
gsub("(^|-)0+(?!-|$)", "\\1", codes, perl=TRUE)

See R proof. See the regex proof.

Expression explanation

--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    ^                        the beginning of the string
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    -                        '-'
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  0+                       '0' (1 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    -                        '-'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )                        end of look-ahead
Related