List <TextBlock> FindRegexBlocks (Regex rxStart, Regex rxEnd, CheckBlockEnd checkEnd, IList blocks, TextBlockType typeForMatches, bool discardBlocks)
{
var ret = new List <TextBlock> ();
foreach (TextBlock block in blocks) {
if (block.Type != TextBlockType.Verbatim) {
ret.Add (block);
continue;
}
int lastIndex = 0, index;
MatchCollection matches = rxStart.Matches (block.Content);
bool foundMatches = matches.Count > 0;
foreach (Match match in matches) {
foundMatches = true;
index = match.Index;
if (lastIndex < index)
ret.Add (new TextBlock (TextBlockType.Verbatim, block.Content.Substring (lastIndex, index - lastIndex)));
string value = match.Value;
if (rxEnd != null && checkEnd (value)) {
int startFrom = index + value.Length;
Match m = rxEnd.Match (block.Content, startFrom);
if (m.Success)
value += block.Content.Substring (startFrom, m.Index - startFrom) + m.Value;
}
if (!discardBlocks)
ret.Add (new TextBlock (typeForMatches, value));
lastIndex = index + value.Length;
}
if (lastIndex > 0 && lastIndex < block.Content.Length)
ret.Add (new TextBlock (TextBlockType.Verbatim, block.Content.Substring (lastIndex)));
if (!foundMatches)
ret.Add (block);
}
return ret;
}