R:从字符串的开头删除前导零

pan*_*man 6 regex r list character leading-zero

我首先提到了这个问题,但答案对我的情况没有帮助.

我有一个列表,其中每个组件包含以数字开头的元素,后跟单词(字符).元素开头的一些数字有一个或多个前导零.这是列表的一小部分:

x <- list(el1 = c("0010 First",
                  "0200 Second",
                  "0300 Third",
                  "4000 Fourth",
                  "0 Undefined",
                  "60838 Random",
                  "903200 Haphazard"),
          el2 = c("0100 Hundredth",
                  "0200 Two hundredth",
                  "0300 Three hundredth",
                  "0040 Fortieth",
                  "0 Undefined",
                  "949848 Random",
                  "202626 Haphazard"),
          el3 = c("0010 First",
                  "0200 Second",
                  "0300 Third",
                  "0100 Hundredth",
                  "0200 Two hundredth",
                  "0300 Three hundredth",
                  "0 Undefined",
                  "60838 Random",
                  "20200 Haphazard"))
Run Code Online (Sandbox Code Playgroud)

我想要实现的是删除它们可用的前导零,并且在开头0 Undefined加上所有其他不以前导零开头的元素仍然具有单个零.也就是说,将列表如下:

x <- list(el1 = c("10 First",
                  "200 Second",
                  "300 Third",
                  "4000 Fourth",
                  "0 Undefined",
                  "60838 Random",
                  "903200 Haphazard"),
          el2 = c("100 Hundredth",
                  "200 Two hundredth",
                  "300 Three hundredth",
                  "40 Fortieth",
                  "0 Undefined",
                  "949848 Random",
                  "202626 Haphazard"),
          el3 = c("10 First",
                  "200 Second",
                  "300 Third",
                  "100 Hundredth",
                  "200 Two hundredth",
                  "300 Three hundredth",
                  "0 Undefined",
                  "60838 Random",
                  "20200 Haphazard"))
Run Code Online (Sandbox Code Playgroud)

我现在已经好几个小时都没有成功.我能做的最好的就是:

lapply(x, function(i) {
  ifelse(grep(pattern = "^0+[1-9]", x = i),
         gsub(pattern = "^0+", replacement = "", x = i), i)
})
Run Code Online (Sandbox Code Playgroud)

但是,它只返回列表组件中存在前导零的那些元素,但不返回没有和没有的其余元素0 Undefined.

有人可以帮忙吗?

akr*_*run 7

我们遍历list(lapply(x, ..)),用于sub替换list元素中的前导零.我们匹配字符串(^0+)后面的一个或多个零,后跟正面正则表达式lookahead((?=[1-9]))指定的数字1-9 ,并将其替换为''.

lapply(x, function(y) sub('^0+(?=[1-9])', '', y, perl=TRUE))
Run Code Online (Sandbox Code Playgroud)

或者如评论中提到的@hwnd,我们可以使用捕获组,而不是lookahead.

lapply(x, function(y) sub('^0+([1-9])', '\\1', y))
Run Code Online (Sandbox Code Playgroud)

或者不使用匿名函数,我们可以指定pattern和的replacement参数sub

lapply(x, sub, pattern='^0+([1-9])', replacement='\\1')
Run Code Online (Sandbox Code Playgroud)

  • 您可以省略`perl = TRUE`参数,而不是真正需要的参数.`lapply(x,function(y)sub('^ 0 +([1-9])','\\ 1',y))` (2认同)