Free Support Forum - aspose.com

Finding Embedded Text

I am looking for a way to do searches for a particular type of text; any embedded in square brackets: [test1]. A normal search & replace will not work: I need to evaluate the contents of each tagged area before determining what to replace it with.

How can I do a find based on the start bracket and end bracket (length of text inside tag will vary), evaluate the text, and then replace the bracketed area?

Thanks!

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think that you can use regular expressions. Also you can can use ReplaceEvaluetor. For example see the following code:

public void Test214()

{

//Open document

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

//Create rexeg

Regex regexFindPlaceholder = new Regex(Regex.Escape("[") + @"(?\S+)" + Regex.Escape("]"));

//find and replace

doc.Range.Replace(regexFindPlaceholder, new ReplaceEvaluator(Replace214), false);

//Save output document

doc.Save(@"Test214\out.doc");

}

ReplaceAction Replace214(object sender, ReplaceEvaluatorArgs e)

{

switch (e.Match.Groups["name"].Value)

{

case "FirstName":

e.Replacement = "Alexey";

break;

case "LastName":

e.Replacement = "Noskov";

break;

}

return ReplaceAction.Replace;

}

The document that I used for testing contains [FirstName] and [LastName] placeholders.

I hope this could help you.

Best regards