Python - finding date in a string

前端 未结 5 1944
误落风尘
误落风尘 2021-02-20 12:24

I want to be able to read a string and return the first date appears in it. Is there a ready module that I can use? I tried to write regexs for all possible date format, but it

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-20 12:38

    Here, I suppose that you want to parse dates in different formats (and perhaps even languages). If you just need the datestring out of some text, use dateutil like the other commenters recommend...

    I had this task some time ago as well, and I used pyParsing for creating a parser based on my requirements, although any decent parser should do. It is far easier to read, test and to debug than regular expressions.

    I do have some (although crappy) example code on my blog that aims to find date expressions in USA format and German format alike. It may not be what you need, but it's pretty well adjustable.

提交回复
热议问题