Hello,
I’m trying to get some text that is surrounded by “<%CLAUSE” and “%>”. In order to do that, I use the Replace method of the Range class with the following regular expression “<%CLAUSE .+%>”. My problem is that the Range function only locate the first occurrence and not all of them.
How could I have all the occurrences ?
Here is the code I use :
private void CreateClausesList()
{
clausesList = new List();
Regex clauseRegex = new Regex("<%CLAUSE .+%>");
document.Range.Replace(clauseRegex, new ReplaceEvaluator(ReplaceEvaluator), true);
}
private ReplaceAction ReplaceEvaluator(object sender, ReplaceEvaluatorArgs e)
{
Run run = (Run)e.MatchNode;
int matchOffset = e.MatchOffset;
string clauseText = run.Text.Substring(matchOffset);
if (clauseText.IndexOf("%>") != -1)
{
clauseText = clauseText.Substring(0, clauseText.IndexOf("%>") + 2);
}
else
{
while (true)
{
run = (Run)run.NextSibling;
string runText = run.Text;
if (runText.IndexOf("%>") != -1)
{
clauseText += runText.Substring(0, runText.IndexOf("%>") + 2);
break;
}
else
{
clauseText += runText;
}
}
}
clauseText = clauseText.Replace("<%CLAUSE", "").Replace("%>", "").Trim();
clausesList.Add(clauseText);
return ReplaceAction.Skip;
}
Thank you.