1

I am using dateparser modules to parse some human readable text to python date time objects. For now I am having a list of regex strings and applying on input strings to find out is there any matches.

I want to use full list of regex list that dateparser uses, is there way to get the list or is there a way to work on input string only.

I am having list like below

date_regex = ['[0-9]+ days', 'yesterday', '[0-9]+ weeks', 'a week', '1 week', 'last week', '[0-9]+ months', 'a month', '1 month', 'year']

but using dateparser can parse strings like 1 year ago, last 3 mins ago. So I would like to take all available regex that dateparser using.

My input contains lot of text also included with date/time related strings as well. So I need to extract those and convert them into date time object using dateparser.

Update with example input="I booked a movie 4 days ago, but planned last week". I need to extract 4 days ago and last week. I thought of applying all available regex strings in dateparser.

kishore
  • 413
  • 1
  • 4
  • 20

1 Answers1

1

Why don't you just use the dateparser package? Parsing dates from natural language is pretty complex with lots of edge cases. If you are just curious what they are doing, the best way to find out is to look at the source code.

Edit:

You can convert a list of strings into regexes:

import re
regexes = list(map(lambda x: re.compile(x), items))
MCH
  • 2,124
  • 1
  • 19
  • 34
  • Yeah I am looking in their source code that is there a method they have used to check user passed input string over their regex matcher. – kishore May 10 '18 at 11:14
  • @kishore is the question, how do you read a regex from file and then execute it on a variable? – MCH May 10 '18 at 11:20
  • I think the question is, "how do I use `dateparser`". – alexis May 10 '18 at 11:30
  • No I want to apply all possible regex on my input string to find any date/time related strings are there or not. if there convert them into date time object. – kishore May 10 '18 at 11:31
  • @kishore You can do that using the `dateparser` package. The details are in the readme. https://github.com/scrapinghub/dateparser#usage – MCH May 10 '18 at 11:36