Here are the 4 RE's that I have come up with to parse dates from a string.
These take into account different seperators and languagues that have
2 words for a month.
(\d+(?<mark>[-| |/|\.])[^\d|^ ]+ *[^\d|^ ]*\k<mark>\d+)
(\d+ *(?<mark>[ |/|\.|-]) *\d+ *\k<mark> *\d+)
([a-zA-Z]+[\.]* +\d+ *, *\d+)
([a-zA-Z]+[\.]* +[a-zA-Z]* *\d+ *, *\d+)
As with most things there is more than one way of doing it. This is what
worked for me.
You can take the results and pass it into the Parse() to see if it is a valid date.
try {
CultureInfo cinfo = new CultureInfo(name, false);
date = DateTime.Parse(REresultText, cinfo.DateTimeFormat);
}
Cheers,
Dave
On Tue, 6 Jul 2004 16:32:40 +0300, "George Ionescu" <(E-Mail Removed)> wrote:
>Thanks Jakob.
>
>I'll go for RegEx, then.
>
>Regards,
>George Ionescu
>
>"Jakob Christensen" <(E-Mail Removed)> wrote in message
>news:11ABB8D2-7C09-4564-9FDE-(E-Mail Removed)...
>> Hey George,
>>
>> I don't think there is an easy way to do this. Regular expressions may be
>the best solution. You can also use the property
>DateTimeFormatInfo.DateSeparator to specify which date separator you wish to
>use. But I don't think there is any built-in way for supporting more that
>one separator unless you want to try parsing the string with a number of
>different separators using DateTimeFormatInfo.DateSeparator.
>>
>> Regards, Jakob.
>>
>>
>> "George Ionescu" wrote:
>>
>> > Hello Jakob,
>> >
>> > Thanks, that worked. One other question: I'd like to support different
>date
>> > separators (e.g. dot, comma, slash); what should I use to parse a string
>and
>> > construct a date? Should I go for regular expressions or is there
>another
>> > way (date.ParseExact only allows one format).
>> >
>> > Thank you.
>> > Regards,
>> > George Ionescu
>> >
>> > "Jakob Christensen" <(E-Mail Removed)> wrote in message
>> > news:A05DE967-72A6-457D-A9D3-(E-Mail Removed)...
>> > > Hey George,
>> > >
>> > > I guess this is how Microsoft thinks that dates are formatted in
>Romania
>> > :-) If you do not like this format you can change it using
>> > DateTimeFormatInfo.AbbreviatedMonthNames:
>> > >
>> > > DateTimeFormatInfo dfi = (new CultureInfo("Ro-ro")).DateTimeFormat;
>> > > DateTime now = DateTime.Now;
>> > > dfi.AbbreviatedMonthNames = new string[13] { "ian", "feb", "mar",
>"apr",
>> > "mai", "iun", "iul", "aug", "sep", "oct", "nov", "dec", "" };
>> > > string strDate = now.ToString("dd-MMM-yyyy", dfi);
>> > >
>> > > Regards, Jakob.
>> > >
>> > >
>> > > "George Ionescu" wrote:
>> > >
>> > > > Hello all,
>> > > >
>> > > > given the following piece of code:
>> > > >
>> > > > DateTimeFormatInfo dfi = (new CultureInfo("Ro-ro")).DateTimeFormat;
>> > > > DateTime now = DateTime.Now;
>> > > > string strDate = now.ToString("dd-MMM-yyyy", dfi);
>> > > >
>> > > > the strDate contains the string '21-iul.-2004'
>> > > >
>> > > > What's with the dot after the month's name ? Am I doing something
>wrong
>> > here
>> > > > ?
>> > > >
>> > > > Thank you.
>> > > > Regards,
>> > > > George Ionescu
>> > > >
>> > > >
>> > > >
>> >
>> >
>> >
>
|