Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Can anybody give me the regular expression for a string followed by a date.I can use this regular expression in c# code page.

for example "Date of Birth : 07/12/1989"
Posted
Comments
Prasad_Kulkarni 2-Apr-12 2:01am    
Not clear.
Kiirrii 2-Apr-12 2:08am    
try this..
(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d

C#
string input = "Date of Birth : 07/12/1989";
Regex regex = new Regex(@"(?<info>.+)\s*:\s*(?<date>\d+/\d+/\d+)");
Match match = regex.Match(input);
if (match.Success)
{
    string info = match.Groups["info"].Value;
    string dateString = match.Groups["date"].Value;
    DateTime date = DateTime.Parse(dateString, System.Globalization.DateTimeFormatInfo.InvariantInfo);
}
 
Share this answer
 
Comments
sharanya.alladi 2-Apr-12 3:17am    
Thanx for the solution.. It is working fine with only date formate like '07/12/1989'.I need the regularexpression which satisfy all the date formates.thanks..
Bernhard Hiller 2-Apr-12 4:00am    
"All the date formats": that is not possible.
sharanya.alladi 2-Apr-12 4:08am    
All the date formats means Indian date formats like 'Date of Birth : 07/12/1989' or
'Date of Birth : 07-12-1989' or
'Date of Birth : 07th Dec 1989'.Thanks.
Muralikrishna8811 2-Apr-12 5:39am    
Hi I hope you need to check them in each way possible...

means first need to check with one format if it not match thn check with another format like that.
sharanya.alladi 2-Apr-12 6:48am    
ok thanks... Now I'm using "(?<info>.+)\s*:\s*(?<date>\d+[-/.]\d+[-/.]\d+)".But i didn't get regular expression for the strings like 'DOB: 12th may 2012'.Can you suggest me any regular expression for this.. Thanks
Hi


Here i just did some code for your requirement.

this is just for testing only.

C#
string Datestring = "Date of Birth : 07/12/1989";
   if (Datestring.IndexOf(':') > 0)
   {
       string datestr = Datestring.Split(':')[0];
       string datefrmt = Datestring.Split(':')[1];
        //you can test this based on REgEp also.
       if (datefrmt.IndexOf('/') > 0)
       {
           datefrmt = datefrmt.Trim();
           string date1 = datefrmt.Split('/')[0];
           string mnt = datefrmt.Split('/')[1];
           string yr = datefrmt.Split('/')[2];
           int datr, mntr, yre;
           if (int.TryParse(date1, out datr) && int.TryParse(mnt, out mntr) && int.TryParse(yr, out yre))
           {
               //entered string matched with one of date format
           }
       }
       else
       {
           if (datefrmt.IndexOf('-') > 0)
           {
               //Date of Birth : 07-Dec-1989
           }
           else
           {
               //Date of Birth : 07th Dec 1989
               string date1 = datefrmt.Split(' ')[1];
               string mnt = datefrmt.Split(' ')[2];
               string yr = datefrmt.Split(' ')[3];
               int datr, mntr, yre;
               int monthIndex;

               string[] MonthNames = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
               monthIndex = Array.IndexOf(MonthNames, mnt) + 1;

               if (int.TryParse(date1.Replace("th",""), out datr) && (monthIndex>0) && int.TryParse(yr, out yre))
               {
                   //entered string matched with one of date format
               }
           }
       }
   }


In the above code you need to change some to achieve your requirement this is just for giving you idea how to do.

and i did exactly string what you want with white spaces also.

I hope you understood what I did.

you can reduce that code using Regular Expressions.

All the Best
 
Share this answer
 
Comments
sharanya.alladi 2-Apr-12 7:15am    
Thanks..
Use the regex to match the label and the date text after the label. Then employ DateTime.TryParse(...). See the DateTime.TryParse(...)[^] - there are good examples about the issues on parsing "any" date format (basically: not possible in general: you have to make a choice on what culture(s) youo want to try parsing, and what the time zone is (it this is important to your solution).

C#
var match = Regex.Match(s, "^Date of Birth\s*:\s*(.*?)\s*$");
if (match.Success)
{
    DateTime dt;
    if (DateTime(match.Groups[1].Value, out dt))
    {
        // store it
        ...
    }
    else
    {
        // try another culture or error
        ...
    }
}
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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