Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
How to extract date part only from a English sentence.
Some scenario

Is there is any holiday on 19-02-2012
Is there is any holiday on 19/02/2012
Is there is any holiday on 19.02.2012
Is there is any holiday on 2012/02/19
Is there is any holiday on 02-19-2012
Is there is any holiday on 19th feb 2017
Is There is any holiday on 19th-feb-2017
Is There is any holiday on Feb 19 2017
Is There is any holiday on 19th/feb/2017
Is There is any holiday on 19 Feb
Is There is any holiday on Feb 19

For these all sentences expected output should be in Date format
Day - 19
Month - 02
Year -2017

What I have tried:

We used regular expressions of [^]

but not able to cover all cases.
Updated 18-Apr-17 18:47pm
Mehdi Gholam 14-Apr-17 3:08am    
Just make a different regex for all the cases.
Sudheer Kumar Tiwari 14-Apr-17 3:25am    
Thanks Mehdi,

I hope it's not a good approach. I need a generic solution. Have u know about any parser beside stanford and lexical. That we can use.
Sinisa Hajnal 14-Apr-17 7:28am    
It is good approach and it IS generic. You will never be able to cover natural language in full by regex. But if you separate different cases, you will make it more readable and easily recognizable for any future developer. One other option is to use Microsoft Luis (or any other bot) that can be trained on sentence templates.
Peter_in_2780 14-Apr-17 4:15am    
Check out NATTY. There are online sites to play with it, and a github repo.
Nick_3141592654 14-Apr-17 4:51am    
Do you ONLY need to cope with a sentence of the form "Is There a holiday on<date>" ?

If so, then you have a chance of achieving this. If not then you've no chance. For example, the sentence "I asked April if she May be able to take a holiday on July 14th, but she said no because that's her sister June's birthday".

May be there any tool, but if you want to do it manually you should use some small steps.

1. find the date in sentence.
2. make substring of the sentence as having only date part.
3. because date are in different formats, you should change different find criteria for different date format. You can find by year or by month name or simply use the same date format if possible.

String s1 = "Is there is any holiday on 19-Feb-2012";
int i = s1.IndexOf("Feb")
String date = s1.Substring(i-3, 11);
Share this answer
Sudheer Kumar Tiwari 19-Apr-17 7:16am    
Thanks Bhola Ram Sahu,
Actually i am looking for some generic solution. That it should work with all format of dates. For this i am writing a library to handle all dates parts using Regx and some parser.
Bhola Ram Sahu 19-Apr-17 9:26am    
You can try SOLUTION 2 by Midi_Mick. It works. I tried it. It will definately suit your requirement
I had a similar issue, and eventually came up with a Regex that did the trick. See my QA here[^] (Solution 4 does the trick)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900