Date in String.  
Author Message
Osprey





PostPosted: Regular Expressions, Date in String. Top

Hi all,

I have some problems in regards to detecting a date in a string.

I have a text box which user can enter anything in it. But I would like to detect dates. For example, if user enter " I would like to make an appointment on 12 Jun 2006", i want my system to automatically detects that there's a date in that particular string.

How is that possible to do so . I have tried using the .Contain() method but it wanted me to pass in the value. but i do not want to pass in any value. I just want to detect that there's a date therefore i am able to extract the date out and do something else with it.

Thanks alot

George.



.NET Development19  
 
 
The Bad Girl





PostPosted: Regular Expressions, Date in String. Top

Hello,

Do you really need to do that There's a better way: why don't you use a datetime picker control Much easier and the user can choose the date and you will know for sure.

Regards,



 
 
boban.s





PostPosted: Regular Expressions, Date in String. Top

That is cool idea but really hard to implement. You must set some rules to achive start results. For example, date must be at specific position, in this case at the end of line. Second, date must be in specific format, in this case dd MMM yyyy. On text validation event or some other event you will execute TryParse method of DateTime class to check if there is a date at the end of line with that format. Or you can search the whole string and find the Month and if you did find month then check from left and right of that position, did you have a valid date.

 
 
Andrej Tozon





PostPosted: Regular Expressions, Date in String. Top

Hi,

you could use regular expressions to extract date patterns from your text. The following code would search for a date pattern you described and return all the matches. Regular expression patern will probably require a bit more tweaking to handle all the date variations  you want to handle in your application, this is just an example:

ParseDates("I went on a walk on sunday, 12 may 2003");
ParseDates(
"I went on a trip on 1 August 2000 and returned on 7 May 2005");

public void ParseDates(string text)
{
   
MatchCollection dates = Regex.Matches(text,
, RegexOptions.IgnoreCase);
    foreach (Match date in
dates)
    {
       
MessageBox
.Show(date.Value);
    }
}

Of course, after getting this kind of text out of your string, you should check if it really is a date or not (using Date.TryParseExact method). Something like:

ParseDates("I went on a walk on sunday, 12 may 2003");
ParseDates(
"I went on a trip on 1 August 2000 and returned on 7 May 2005");
ParseDates(
"I wanted to do something on 31 february 2004"); // Invalid

public void ParseDates(string text)
{
   
MatchCollection dates = Regex.Matches(text, , RegexOptions.IgnoreCase);
   
foreach (Match date in dates)
   
{
       
DateTime realDate;
       
if (DateTime.TryParseExact(date.Value, "d MMMM yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out realDate))
       
{
            MessageBox.Show(realDate.Date.ToShortDateString());
       
}
   
}
}

Andrej