Thursday, 15 July 2010

regex - C#: Split string on date keeping date intact -



regex - C#: Split string on date keeping date intact -

i have next 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 output next 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 following:

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

this gives array of dates. how can split string on dates?

you can split string on empty string before date. need regex:

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

splitting on above regex, give output want. split string on empty string preceded date of form - 21/10/2012, , not preceded digit. need look-behind stuff, doesn't tear day part apart. without that, split on empty string before 1 in 21, keeping 2 , 1/10/2012 separate element.

also, note empty string first element of array, since first empty string in string satisfies split criteria.

validating dates can complex regex. specially, if want restrict every possible invalid date, 30 feb. still, if want can seek out regex, match 30 & 31 feb , 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}))");

c# regex

No comments:

Post a Comment