[Python] Regex for extracting different data formats

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    I was looking into this question, and trying to adapt the same problem. I have to extract all data inserted into a txt file. So far i have reached the following (barely readable) pattern

    import re
    pattern = r'(?<![A-Za-z0-9-\./])\b(?:\d{1,2}[-/th|st|nd|rd\s.])?(?:(?:Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|August|Sep|September|Oct|October|Nov|November|Dec|December)[\s,.]*)?(?:(?:\d{1,2})[-/th|st|nd|rd\s,.]*)?(?:\d{2,4})\b(?![A-Za-z0-9-/])'
    dates = df.apply(lambda row: [match for match in re.findall(pattern, row)])


    But alongside my text file there are some numbers being catched, for instance 29505 would be catched by the regEx, but I have been unsuccessful in fixing it so that 2,3,5 or more digit numbers are not considered. Any hints on what can I do? Is it easier to drop those on a post filter?

    Continue reading...

Compartilhe esta Página