I’m writing a django application to collect and analyse log files from various different applications. I want the user to be able to supply either a date format, or a regex that will match the date format, so that I can track the start and end time of the file.
I’d rather not have the user supply both a regex and a date format, but I’m thinking there isn’t a way around it.
I want to extract the first and last datetime within the file, and parse it into a datetime object within python. As I said though, the format of the date will be user-supplied in some way.
Is there a way to build out a regex from a date format or vice versa? Or am I just going to require a format and a pattern?
If your users can supply a dateformat in strptime format, there is no need to use regular expressions as well.
strptimeis flexible enough to handle most date formats.EDIT
Since
strptimedoes not allow for searches in arbitrary text, you will probably need a regular expression. It is not trivial to turn a strptime pattern into a regular expression, but Fredrik Lundh did it for you. You can find the code here: http://effbot.org/librarybook/time.htmLook for the phrase: “Example: A strptime implementation”