Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Main()
- {
- var lines = File.ReadAllLines(@"d:\temp\input.txt");
- var re = new Regex(@":(?<type>Allocation|Deallocation).*Item\s*:(?<id>\d+)\s*$", RegexOptions.IgnoreCase);
- var matches = new Dictionary<int, ReferenceMatch>();
- foreach (var line in lines)
- {
- Match ma = re.Match(line);
- if (!ma.Success)
- continue;
- int id = int.Parse(ma.Groups["id"].Value);
- matches.TryGetValue(id, out ReferenceMatch refMatch);
- if (StringComparer.CurrentCultureIgnoreCase.Equals(ma.Groups["type"].Value, "Allocation"))
- refMatch.Allocated = true;
- else
- refMatch.Deallocated = true;
- matches[id] = refMatch;
- }
- using (var writer = new StreamWriter(@"d:\temp\output.txt"))
- {
- foreach (var line in lines)
- {
- Match ma = re.Match(line);
- if (!ma.Success)
- {
- writer.WriteLine(line);
- continue;
- }
- int id = int.Parse(ma.Groups["id"].Value);
- matches.TryGetValue(id, out ReferenceMatch refMatch);
- if (refMatch.Allocated && refMatch.Deallocated)
- continue;
- writer.WriteLine(line);
- }
- }
- matches.Dump();
- }
- public struct ReferenceMatch
- {
- public bool Allocated;
- public bool Deallocated;
- }
Add Comment
Please, Sign In to add comment