Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split string with date in c#

Tags:

c#

asp.net

i have string with date , i want to split it with date and string

For example : I have this type of strings data

9/23/2013/marking abandoned based on notes below/DB
12/8/2012/I think the thid is string/SG

and i want to make it like as

9/23/2013     marking abandoned based on notes below/DB
12/8/2013     I think the thid is string/SG

so, i don't know how to split these strings and store in different columns of table. pls help me.

like image 412
Nikunj Avatar asked Dec 20 '22 22:12

Nikunj


1 Answers

string[] vals = { "9/23/2013/marking abandoned based on notes below/DB",
                  "12/8/2012/I think the thid is string/SG" };

var regex = @"(\d{1,2}/\d{1,2}/\d{4})/(.*)";

var matches = vals.Select(val => Regex.Match(vals, regex));

foreach (var match in matches)
{
    Console.WriteLine ("{0}     {1}", match.Groups[1], match.Groups[2]);
}

prints:

9/23/2013     marking abandoned based on notes below/DB
12/8/2012     I think the thid is string/SG

(\d{1,2}/\d{1,2}/\d{4})/(.*) breaks down to

  • (\d{1,2}/\d{1,2}/\d{4}):
    • \d{1,2} - matches any one or two digit number
    • / - matches to one / symbol
    • \d{4} - matches to four digit number
    • (...) - denotes first group
  • (.*) - matches everything else and creates second group
like image 106
Ilya Ivanov Avatar answered Jan 06 '23 02:01

Ilya Ivanov