Reputation: 55
I have a string like such: "07/04/2017 16:09:03 by Joe Bloggs) Added 07/04/2017 17:03:04 by Joe Bloggs) Updated"
I'd like to split the string into a list of comments. The string example above contains two separate comments that I'd like to split into a list. I've identified that each new comment starts with a date. So for each date, id like to split the string.
I have attempted to use the following Regex pattern '\d{1,2}\/\d{1,2}\/\d{4}' which detects the date.
var sqlComments = "07/04/2017 16:09:03 by Joe Bloggs) Added 07/04/2017 17:03:04 by Joe Bloggs) Updated"
var comments = Regex.Split(sqlComments, RegexDateIdentifier);
return comments.Where(c => c != string.Empty).ToList();
However the code above, splits the string as I'd like, but removes the date. So I am left with like;
Can anyone advise how to perform the above but with keeping the date (the deliminator)?
Upvotes: 1
Views: 45
Reputation: 25330
This should do what you are after, it uses a Zero-Width Positive Lookahead Assertion plus a \s+
to remove the spaces:
static void Main(string[] args)
{
string RegexDateIdentifier = @"\s+(?=\d{1,2}\/\d{1,2}\/\d{4})";
var sqlComments = "07/04/2017 16:09:03 by Joe Bloggs) Added 07/04/2017 17:03:04 by Joe Bloggs) Updated";
var comments = Regex.Split(sqlComments, RegexDateIdentifier);
var list = comments.ToList();
foreach (var s in list)
{
Console.WriteLine("|{0}|", s);
}
}
Upvotes: 1