We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replacement string for Ranger replace with regular expression

I am doing a range replace with the characters in sr. How do I refer back to the grouping in the replace. For example, if the found string is &pa I want to replace it with a. If it is &pd, I want to replace it with d.

Aspose.Words.Document doc = new Aspose.Words.Document(textBox1.Text);
String sr = @"&p\s*([a-z])";
Regex rg = new Regex(sr);


@granite61 Please, consider the following solution.

string sr = @"&p\s*([a-z])";
Regex rg = new Regex(sr);
doc.Range.Replace(rg, "", 
    new FindReplaceOptions { ReplacingCallback = new ReplacingCallback() });


private class ReplacingCallback : IReplacingCallback
    public ReplaceAction Replacing(ReplacingArgs args)
        args.Replacement = args.Match.Groups[1].Value;
        return ReplaceAction.Replace;

Or you can use UseSubstitutions instead of ReplacingCallback (see more).

doc.Range.Replace(regex, "$1", 
    new FindReplaceOptions { UseSubstitutions = true });

I found the answer to my question.