It is necessary to check in Ruby a line like '12.01.2016 14:26' , whether the date / time is correct in it.

I assume that this is easiest to do with a regular expression .

  • If a regular expression, how ?
  • If not a regular expression, then how ?
  • one
    And what have you tried to do? What exactly did not work out? - Dmitriy Simushev

2 answers 2

Some people, faced with a problem, think: "Exactly, I will use regular expressions." Now they have two problems.
- Jamie Zavinsky

You do not need regular expressions to solve this problem.

The solution is in the standard Ruby library. This is Date.strptime , which accepts a string and parses it according to a strftime mask :

 DateTime.strptime('12.01.2016 14:26', '%d.%m.%Y %H:%M') # => #<DateTime: 2016-01-12T14:26:00+00:00 ((2457400j,51960s,0n),+0s,2299161j)> 

If the date / time is incorrect, an ArgumentError exception will arrive.

     (\d\d)\.(\d\d)\.(\d\d\d\d)\s+(\d\d)\:(\d\d) 

    This is exactly your example.

    • He will mistakenly accept the "zero zero" as a valid date. - D-side