Files contain date formats 4 types:

31.12.2017 23:59:59 31-12-2017 23:59:59 2017.12.31 23:59:59 2017-12-31 23:59:59 

How to filter the necessary using regex?

I tried to use

 [\d-\.]+ [\d:]+ 

Since, by analogy, I had a regex for the format of 2017-12-31 24:59:59

 [\d-]+ [\d:]+ 

But python gives the error:

 raise error, v # invalid expression sre_constants.error: bad character range 

How to fix regex?

1 answer 1

This regular expression divides the date into 4 groups: the first three are the date, the fourth is time (note that it is not parsed, that is, it is str = "24:59:59" ):

 (\d+)[\.|\-](\d+)[\.|\-](\d+)\s(.*) #Первый вариант (\d{2,4})[\.|\-](\d{2})[\.|\-](\d{2,4})\s(.*) #Второй вариант 

If you want to catch exactly the dates of this format 2017-12-31 24:59:59, then:

 (\d{4})\-(\d{2})\-(\d{2})\s(.*) 

And just in case, leave it here.

  • Thanks, it helps a lot. But why did you leave time as (. *). Is it also impossible to convert it to regex - Predicate
  • You mean that you can also parse it, but I propose to do this to you, on the Internet a lot of online services for composing regular expressions. And how to do? Look at the date and do it by analogy, there will be questions - ask, but the main thing is that you learn how to work with it. Here is a wonderful service regex101.com - koshachok