Is there a shorter way to extract a date from a string?

前端 未结 4 2130
轮回少年
轮回少年 2021-01-12 02:14

I wrote code to extract the date from a given string. Given

  > \"Date: 2012-07-29, 12:59AM PDT\"

it extracts

  > \"         


        
4条回答
  •  粉色の甜心
    2021-01-12 02:47

    Regex with backreferencing works:

    > sub("^.+([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]).+$","\\1","Date: 2012-07-29, 12:59AM PDT")
    [1] "2012-07-29"
    

    But @Dirk is right that parsing it as a date is the right way to go.

提交回复
热议问题