Split de un texto a partir de fecha

7 views
Skip to first unread message

Carlos Admirador

unread,
Jun 23, 2021, 4:32:01 PMJun 23
to AltNet-Hispano
Tengo un texto así:

2021-06-22 09:21:24 - marking abandoned based on notes below 2021-06-22 13:11:41 - think the thid is string/SG 2021-06-23 19:21:11 - texto

Cómo hacer un split del texto por las fechas ?

Quedaría así:

2021-06-22 09:21:24 - marking abandoned based on notes below 
2021-06-22 13:11:41 - think the thid is string/SG 
2021-06-23 19:21:11 - texto

Carlos Admirador

unread,
Jun 24, 2021, 4:28:30 PMJun 24
to AltNet-Hispano

una solución rápida, pero depende (performance/precisión de validacion de la fecha, legibilidad) haría q cambie la solución

static string SplitAsStringFromDates(string text)
{
var matches = Regex.Matches(text, @"[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}", RegexOptions.Compiled);
 var sb = new StringBuilder();
Match matched = null;
for (int i = 0; i < matches.Count; i++)
{
matched = matches[i];
if (i == matches.Count - 1)
sb.AppendLine(text.Substring(matched.Index));
else
sb.AppendLine(text.Substring(matched.Index, matches[i + 1].Index - matched.Index));
}
return sb.ToString();
}


Reply all
Reply to author
Forward
0 new messages