Stringr - 从连字符两侧删除前导零

Stringr - removing leading zero from both sides of hyphen

对于每个标题,我正在寻找一种方法来删除连字符两边的前导零。

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

我想可以将数字一分为二,删除前导零,然后粘贴在一起,但我想知道是否有更精细的方法使用单步 str_extract()str_replace() 或同类。

编辑:

我目前正在使用

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

并且很好奇是否有可以在单行中实现的替代方法。

您可以使用

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"

参见R demo and the regex demo

\b 匹配字符串开头或非单词字符与单词字符之间的位置,此处为字符串开头或 - 与下一个数字之间。 0+ 匹配一个或多个零,尽可能多。 \B 确保匹配在一个数字之前停止,因此数字中只有零,最后一个保持不变。

您也可以匹配并删除破折号开头或破折号之后的零,但不要匹配和删除破折号末尾或破折号之前的零:

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

参见R proof. See the regex proof

表达式解释

--------------------------------------------------------------------------------
  (                        group and capture to :
--------------------------------------------------------------------------------
    ^                        the beginning of the string
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    -                        '-'
--------------------------------------------------------------------------------
  )                        end of 
--------------------------------------------------------------------------------
  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