I have a string with the repeating pattern of the form
MM/DD/YYYY (FirstName LastName) Status Update: blah blah blah blah
E.G.
string test = "11/01/2011 (Joe Bob) Status Update: Joe is the collest guy on earfth 08/07/2010 (Rach Mcadam) Status Update: whatever I dont care 06/28/2009 (Some Guy) Status Update: More junk and note how I end there's not gonna be another date after me"
How can I group match this so as to have Date, Name, and Status update for each match?
I tried
string datePattern = "\\d{1,2}/\\d{1,2}/\\d{0,4}";
string personPattern = "\\(\\w*\\)";
Regex regex = new Regex("(" + datePattern + ") (" + personPattern + ") (.*)");
MatchCollection matches = regex.Matches(test);
foreach (Match match in matches)
{
Console.WriteLine("##Match Found##");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine(match.Groups[0]);//full text
Console.WriteLine("");
Console.WriteLine(match.Groups[1]);//date only
Console.WriteLine("");
Console.WriteLine(match.Groups[2]);//person
Console.WriteLine("");
Console.WriteLine(match.Groups[3]);//note
}
It’s pulling back nothing at this point.
Spaces aren’t included in
\w, so\w*will not matchJoe Bob. Try changingpersonPatternto"\\([ \\w]*\\)".It also looks like your regex is too greedy, because the
.*at the end will match the rest of the string, instead of stopping at the next date. Try changing your regex to the following: