Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 722
Hashim Aziz Avatar asked Dec 03 '22 13:12

Hashim Aziz


2 Answers

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  
like image 95
sugesh kumar Avatar answered Dec 05 '22 03:12

sugesh kumar


This regex matches your example:

[0-9]{2}[-|\/]{1}[0-9]{2}[-|\/]{1}[0-9]{4}

but if you write wrong date (like 31 Feb) this regex match it.

like image 23
Vladimir Korshunov Avatar answered Dec 05 '22 02:12

Vladimir Korshunov