private static Match[] MatchSubstring(string i_source, string i_matchPattern, bool i_uniqueMatch)
{
//<note> use RegexOptions.Multiline, otherwise it will treat the whole thing as 1 string
Regex regex = new Regex(i_matchPattern, RegexOptions.Multiline);
MatchCollection matchCollection = regex.Matches(i_source);
Match[] result;
if (!i_uniqueMatch)
{
result = new Match[matchCollection.Count];
matchCollection.CopyTo(result, 0);
}
else
{
//<note> cannot use HashSet<Match> because each Match object is unique, even though they may have same value (string). Can use HashSet<string>
//SortedList is more like sorted Dictionary<key, value>
SortedList uniqueMatchCollection = new SortedList();
foreach(Match match in matchCollection)
{
if (!uniqueMatchCollection.ContainsKey(match.Value))
{
uniqueMatchCollection.Add(match.Value, match);
}
}
result = new Match[uniqueMatchCollection.Count];
uniqueMatchCollection.Values.CopyTo(result, 0); //<note> cannot use uniqueMatchCollection.CopyTo(...) since SortedList member is not type-match with destination array member
}
Console.WriteLine("Found {0} matches", result.Length);
return result;
}