Search code examples
regexvalidationdatesublimetext3

Regex to match date formats DD-MM-YYYY and DD/MM/YYYY


I'm working in Sublime Text 3 with a large text file that has entries separated by lines. I'm looking for a regular expression that will:

  • Match entries that appear in the DD-MM-YYYY format
  • Match entries that appear in the DD/MM/YYYY format

If both would be too complicated, then just the first example will do.

I haven't done anything to work towards such a regex, because it's honestly way above my knowledge of it and I wouldn't know where to start, and looking at similar answers dealing with regex validation of date formats confirms those suspicions for me.

Thanks in advance for any help.


Solution

  • This pattern will work for your conditon. Strictly not allowed month to date and vice versa.

    ^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$
    

    This pattern will match these formats: (Date, Month, Year)

    25.04.2017  
    02.04.2017  
    2.4.2017  
    
    25/04/2017  
    5/12/2017  
    15/2/2017  
    
    25-04-2017  
    6-10-2017  
    16-5-2017