views:

95

answers:

2
+3  Q: 

Regex for date.

What should be the regex for matching date of any format like

 26FEB2009 
 30 Jul 2009
 27 Mar 2008
 29/05/2008
 27 Aug 2009

What should be the regular expression for that ?

I have regex that matches with 26-Feb-2009 and 26 FEB 2009 with but not with 26FEB2009. So if any one know then please update it.

(?:^|[^\d\w:])(?'day'\d{1,2})(?:-?st\s+|-?th\s+|-?rd\s+|-?nd\s+|-|\s+)(?'month'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[uarychilestmbro]*(?:\s*,?\s*|-)(?:'?(?'year'\d{2})|(?'year'\d{4}))(?=$|[^\d\w])

The date 26FEB2009 is substring of other string like FUTIDX 26FEB2009 NIFTY 0 and parsed from html page, so I can not set the whitespace or delimiter.

EDIT Examples :

07.11.2008
Jan 11 2008
May 26 2008
26FEB2009
27 Mar 2008
+1  A: 

If it's matching 26 FEB 2009 and not 26FEB2009, sounds like you need to make the whitespace and delimiter character("-" and "/") between each date segment optional.

The + meta character specifies one or more, consider using * (zero or more) for the whitespace.

EDIT

What I meant was, if your regular expression is matching dates with the whitespace/delimiter character, but is not matching the dates without either of them i.e 26FEB2009, then it sounds like you're specifying that the whitespace/delimiter be compulsory for a match.

Here's something I quickly knocked together:

(\d{1,2})(\/|-|\s*)?((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)|\d{2})(\/|-|\s*)?(\d{4})

You might want to check that it's not missing certain features that you want, but it matches all of your examples.

Moonshield
@Moonshield,The date **26FEB2009** is substring of other string like **FUTIDX 26FEB2009 NIFTY 0** and parsed from html page, so I can not set the whitespace or delimiter.
Harikrishna
@Moonshield: Because his months may be `Feb` or `FEB`, I would list the months in the regex as all lower case. The tested string can then be `.ToLower()`.
dboarman
@dboarman-FissureStudios: Good point, the widget I tested the regex with has "case insensitive" turned on.
Moonshield
`Sept` should be `sep` -- you should edit your answer to reflect a more accurate solution ;)
dboarman
@Moonshield,Regex you have defined in the answer gives error like Regex is incomplete.
Harikrishna
@Harikrishna It works in the regex testers I've used, if you're using this in C# you may need to escape each of the backslash characters in the regex string.
Moonshield
+4  A: 

I would advice you against using regex for parsing dates and even strongly against using regex for parsing HTML. For parsing dates you may take a look at the TryParseExact method and for parsing HTML a DOM parser such as Html Agility Pack:

var dateStr = "26FEB2009";
var formats = new[] 
{ 
    "ddMMMyyyy", "dd MMM yyyy", "dd/MM/yyyy"
};
DateTime date;
if (DateTime.TryParseExact(
    dateStr, 
    formats, 
    CultureInfo.InvariantCulture, 
    DateTimeStyles.NoCurrentDateDefault, 
    out date))
{
    // You have a match, use the date object
}
Darin Dimitrov
@Darin Dimitrov,Is it inbuilt method in .net ?
Harikrishna
Yes, I've linked it to MSDN.
Darin Dimitrov
@Darin Dimitrov,Ok..then, we have to just define the different format in the parameter and all the date we define in the for the format parameter will be matched ? Is it open-source dll ? From where I can download it ?
Harikrishna
If you are talking about the `DateTime.TryParseExact` method there's nothing to download, it is already in .NET, if you are talking about Html Agility Pack, click on the link in my answer.
Darin Dimitrov
@Darin Dimitrov,How to write format for date 07.11.2008 ?
Harikrishna
`"dd.MM.yyyy"`.
Darin Dimitrov
@Darin Dimitrvo,How can we use regex here to match any string in which there is a date ?
Harikrishna