Bhushan Firake
Bhushan Firake

Reputation: 9458

C#: Split string on date keeping date intact

I have the following sample data:

21/10/2012 blahblah blah blahblah 265 blah 25 22/10/2012 blahblah blah blahblah 10 blah 14 blah 66 NK blahblah blah blahblah 25

I want the output to be the following data:

21/10/2012 blahblah blah blahblah 265 blah 25
22/10/2012 blahblah blah blahblah 10 blah 14 blah 66 NK blahblah blah blahblah 25

I have tried the following:

var regex = new Regex ("(\d{1,2})/(\d{1,2})/(\d{4})");
var matches = regex.Matches(str);//str is given above
foreach(var item in matches)
{
  //my logic to do operations
}

This gives the array of dates. How can I split the string on dates?

Upvotes: 3

Views: 1061

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213281

You can split your string on empty string before the date. For that you need this regex:

string[] arr = Regex.split(str, "(?<!\d)(?=\d{1,2}/\d{1,2}/\d{4})");

Splitting on the above regex, will give you the output you want. It will split your string on the empty string which is preceded by date of the form - 21/10/2012, and not preceded by a digit. We need to do the look-behind stuff, so that it doesn't tear the day part apart. Without that, it will split on the empty string before 1 in 21, thus keeping 2 and 1/10/2012 as separate element.

Also, note that you will get an empty string as the first element of your array, since the first empty string in your string satisfies the split criteria.


Validating dates can get complex with regex. Specially, if you want to restrict every possible invalid date, like 30 Feb. But still, if you want you can try out this regex, but it will match 30 & 31 Feb and even 31 November.

string[] arr = Regex.split(str, "(?<!\\d)(?=(?:0[1-9]|[1-2][0-9]|[3][01])/(?:0[1-9]|1[0-2])/(?:19[0-9]{2}|2[0-9]{3}))");

Upvotes: 3

Related Questions