?
Solved

RegEx for yyyyMMdd format

Posted on 2010-09-14
6
Medium Priority
?
1,086 Views
Last Modified: 2013-12-16
I have a csv file that has a date column in the following format yyyyMMdd
eg:- 20011114

i want to parse this date via a RegEx into a format that is acceptable by DateTime class in C#

this is what i have done so far (see code section below)

however i am getting an error "Input string was not in a correct format" when the code enters the following line.


if (!ConvertToDate(int.Parse(m.Groups["year"].Value), int.Parse(m.Groups["month"].Value), int.Parse(m.Groups["day"].Value), out date))



m = Regex.Match(str, @"(?<=^(([2-9]\d{3}|1999)((0[1-9]|1[012])(0[1-9]|1\d|2[0-8])|(0[13456789]|1[012])(29|30)|(0[13578]|1[02])31)|(([2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00))0229)$)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            if (m.Success)
            {
                DateTime date;
                if ((defaultFormat ^ DateTimeFormat.USA_DATE) == DateTimeFormat.USA_DATE)
                {
                    if (!ConvertToDate(int.Parse(m.Groups["year"].Value), int.Parse(m.Groups["day"].Value), int.Parse(m.Groups["month"].Value), out date))
                        return false;
                }
                else
                {
                    if (!ConvertToDate(int.Parse(m.Groups["year"].Value), int.Parse(m.Groups["month"].Value), int.Parse(m.Groups["day"].Value), out date))
                        return false;
                }
                parsedDate = new ParsedDateTime(m.Index, m.Length, -1, -1, date);
                return true;

Open in new window

0
Comment
Question by:2ooth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33670084
Assuming that month and date starts from 0
[0-9]{1,4}([0][0-9]|[1][0-2])([0-2][0-9]|[3][0-1])
0
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 1000 total points
ID: 33670103
or just this
Dim D As Date = Date.ParseExact(X, "yyyyMMdd", Nothing);
from here
http://bytes.com/topic/visual-basic-net/answers/616836-type-conversion-date-formatted-yyyymmdd


0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 33670128
You can refer to following link
 
http://www.codeproject.com/KB/recipes/RegexTester.aspx 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
Mathiyazhagan earned 1000 total points
ID: 33670355
I too suggest you to go with DateTime functions as it assures valid date. you can do it with TryParseExact method and  cultureInfo.have attched sample code; hope this helps.
private bool GetValidDate(string str,bool IsUSCulture,out DateTime date)
        {
            System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.InvariantCulture;
            if (IsUSCulture)
                culture = new System.Globalization.CultureInfo("en-US");
            return DateTime.TryParseExact(str, "yyyyMMdd", culture , System.Globalization.DateTimeStyles.None, out date);
        }

//using the above code

 DateTime d;
            if (GetValidDate("20011114", true , out d))
            { 
                //work with valid date d
            }

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33672047
I also agree that you should use the Parse group of methods which are members of the DateTime struct.
0
 

Author Closing Comment

by:2ooth
ID: 33679998
Thanks Guys!!!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question