samedi 18 avril 2015

How to find the matched string with a specific pattern

I need to find out all the strings match the specific pattern from a big string buffer.


For example, I have a string like



"2014:11:12 13:30:05 Tested for ABCD at 12:30 2014:11:12 13:31:05 Tested for ABBB at 12:31 2014:11:12 13:32:05 Tested for ABCC at 12:32"


And I want to have an output like this:



2014:11:12 13:30:05 ABCD 12:30
2014:11:12 13:31:05 ABBB 12:31
2014:11:12 13:32:05 ABCC 12:32


I have tried with similar code like:



string data = "2014:11:12 13:30:05 Tested for ABCD at 12:30 2014:11:12 13:31:05 Tested for ABBB at 12:31 2014:11:12 13:32:05 Tested for ABCC at 12:32";
MatchCollection matches = Regex.Matches("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} Tested for [?]{4} at [0-9]{2}:[0-9]{2}");

/* But matches.count is always 0 here, looks like the pattern [?]{4} not work */

foreach (Match match in matches)
{
string temp = match.Value;
Match dateTime = Regex.Match(temp, "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}");

Match Org = Regex.Match(temp, "Tested for [?]{4}").Value, "[?]{4}");

Match processingTime = Regex.Match(temp, "at [0-9]{2}:[0-9]{2}").Value, "[0-9]{2}:[0-9]{2}");

/* then parse the field datetime, Org and processingTime and write to console */
}

Aucun commentaire:

Enregistrer un commentaire