Free Support Forum - aspose.com

Locate text in a document

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.

Hi

Thanks for your inquiry. I think problem is in your regular expression. You can try using the following code to solve your task.

List<string> clausesList = new List<string>();

public void TestReplace_105628()

{

Document doc = new Document(@"403_105628_OSTENDI\in.doc");

Regex clauseRegex = new Regex(@"<%CLAUSE (?.*?)%>");

doc.Range.Replace(clauseRegex, new ReplaceEvaluator(ReplaceEvaluator_105628), true);

}

private ReplaceAction ReplaceEvaluator_105628(object sender, ReplaceEvaluatorArgs e)

{

Match match = e.Match;

Group textGroup = match.Groups["text"];

string clauseText = textGroup.Value;

clausesList.Add(clauseText);

return ReplaceAction.Skip;

}

I hope that this will help you.

Best regards.

This works.
Thank you very much.