Regex to get date from string

前端 未结 3 756
盖世英雄少女心
盖世英雄少女心 2021-01-25 08:29

I need a regular expression to get date out of the following String

anything-2011.01.17-16.50.19.xml

Is this a correct one

^\\         


        
相关标签:
3条回答
  • 2021-01-25 08:44

    Try with:

    ^.*?-([0-9.]+)-([0-9.]+)\..*$
    

    You should also accept your recent questions.

    0 讨论(0)
  • 2021-01-25 08:44

    The following regex is a bit stricter. It will match years in the range 1900 to 2099, months in 01 to 12 and days in 01 to 31.

    ^.*?-((19|20)\d\d\.(0[1-9]|1[1-2])\.(0[1-9]|[12][0-9]|3[01]))-.*?.xml$
    
    0 讨论(0)
  • 2021-01-25 08:56

    This here is checking the format YYYY.MM.DD-HH.MM.SS

    ^(.*?)-(\d{4}(?:\.\d{2}){2}-\d{2}(?:\.\d{2}){2})\.xml$
    

    But it does not verify if the date or the time is a valid value.

    Online check on regexr.

    0 讨论(0)
提交回复
热议问题