regex to add hypen in dates

前端 未结 2 1159
野的像风
野的像风 2021-01-27 09:26

In R I have a character string that looks like the following

x <- c(\"20130603 00:00:03.102\",\"20130703 00:01:03.103\",\"20130804 00:03:03.104\")


        
相关标签:
2条回答
  • 2021-01-27 09:39

    The following should work:

    gsub("(\\d{4})(\\d{2})(\\d{2})", "\\1-\\2-\\3", subject, perl=TRUE);
    
    0 讨论(0)
  • 2021-01-27 10:00

    i actually came up with the exact same thing as Tim. if you want something more compact try using the stringr package with it.

    library(stringr)
    str_replace_all("(\d{4})(\d{2})(\d{2})", "$1-$2-$3")
    
    0 讨论(0)
提交回复
热议问题