RegEx for a date format

Posted by Ivan on Stack Overflow See other posts from Stack Overflow or by Ivan
Published on 2011-06-29T14:09:05Z Indexed on 2011/06/29 16:22 UTC
Read the original article Hit count: 382

Filed under:
|

Say I have a string like this:

07-MAY-07 Hello World 07-MAY-07 Hello Again

So the pattern is, DD-MMM-YY, where MMM is the three letter format for a month. What Regular Expression will break up this string into:

07-MAY-07 Hello World
07-MAY-07 Hello Again

Using Jason's code below modified for C#,

string input = @"07-MAY-07 Hello World 07-MAY-07 Hello Again";   
string pattern = @"(\d{2}-[A-Z]{3}-\d{2}\s)(\D*|\s)";

string[] results = Regex.Split(input, pattern);
results.Dump();

Console.WriteLine("Length = {0}", results.Count());
foreach (string split in results) 
{
   Console.WriteLine("'{0}'", split);
   Console.WriteLine();
}

I get embedded blank lines?

Length = 7
''

'07-MAY-07 '

'Hello World '

''

'07-MAY-07 '

'Hello Again'

''

I don't even understand why I am getting the blank lines...?

© Stack Overflow or respective owner

Related posts about c#

Related posts about regex