Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我很接近,但似乎无法让它发挥作用。我想通过找到两个数字前面有一个 0 的位置,然后将这个字符串“ 1960-12-031 22 : 00 : 010 ”转换为这个字符串“1960-12-31 22:00:10 ”剥离 0。
我有正则表达式工作:
txt <- "1960-12-031 22:00:010" gsub("(0+[0-9]{2})", "\\1", txt, perl=TRUE)
我似乎无法弄清楚如何处理“\\1”来删除第一个字符。
任何援助将不胜感激 -
txt <- "1960-12-031 22:00:010" gsub("0+([0-9]{2})", "\\1", txt, perl=TRUE)
请注意,这会将 031、0031、00031 等替换为 31。如果您只想删除第一个零,请使用"0{1}([0-9]{2})".
"0{1}([0-9]{2})"
更新:根据评论中的建议,您可以使用"[\\D]0+([0-9]{2})"避免在 2012 等数字中匹配零。
"[\\D]0+([0-9]{2})"