Search code examples
regexregex-group

Matching date, excluding 0 from month


We have files named like the following:
d31_20210315-19232.sql.tar.gz
d31_20201215-19232.sql.tar.gz

Matching the year is easy:
(?<=_)(?P<year>\d{4})

matching the month without the 0 has me coming unstuck:
(0(?P<month>[1-9]))|((?P<month>[1][1-9]))
this isn't allowed but describes what I want to do.

Same with the day I want to ignore the 0 and match either 1-9 or 12-31 inside the group:
(?P<day>

I'm at the stage where I'm going to try and code around the regex capture to get the desired result, but its been bugging me I cannot get this.


Solution

  • You may try this one:

    (?<=_)(?P<year>\d{4})0?(?P<month>\d+)(?=\d\d-)0?(?P<day>\d+)
    
    • (?<=_)(?P<year>\d{4}) matching the year.
    • 0?(?P<month>\d+)(?=\d\d-) matching the month, ignores the 0 if there's any. Also makes sure after the group month, there are still 2 digits with a dash in front.
    • 0?(?P<day>\d+) matching the day, ignore the 0 if there's any.

    Check the test cases