ReplacingArgsGroupIndex Property |
Namespace: Aspose.Words.Replacing
GroupIndex has effect only when GroupName is null.
Default is zero.
public void ReplaceNumbersAsHex() { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.Font.Name = "Arial"; builder.Write( "There are few numbers that should be converted to HEX and highlighted: 123, 456, 789 and 17379."); FindReplaceOptions options = new FindReplaceOptions(); // Highlight newly inserted content with a color options.ApplyFont.HighlightColor = Color.LightGray; // Apply an IReplacingCallback to make the replacement to convert integers into hex equivalents // and also to count replacements in the order they take place options.ReplacingCallback = new NumberHexer(); // By default, text is searched for replacements front to back, but we can change it to go the other way options.Direction = FindReplaceDirection.Backward; int count = doc.Range.Replace(new Regex("[0-9]+"), "", options); Assert.AreEqual(4, count); doc.Save(ArtifactsDir + "Range.ReplaceNumbersAsHex.docx"); } /// <summary> /// Replaces arabic numbers with hexadecimal equivalents and appends the number of each replacement. /// </summary> private class NumberHexer : IReplacingCallback { public ReplaceAction Replacing(ReplacingArgs args) { mCurrentReplacementNumber++; // Parse numbers int number = Convert.ToInt32(args.Match.Value); // And write it as HEX args.Replacement = $"0x{number:X} (replacement #{mCurrentReplacementNumber})"; Console.WriteLine($"Match #{mCurrentReplacementNumber}"); Console.WriteLine($"\tOriginal value:\t{args.Match.Value}"); Console.WriteLine($"\tReplacement:\t{args.Replacement}"); Console.WriteLine($"\tOffset in parent {args.MatchNode.NodeType} node:\t{args.MatchOffset}"); Console.WriteLine(string.IsNullOrEmpty(args.GroupName) ? $"\tGroup index:\t{args.GroupIndex}" : $"\tGroup name:\t{args.GroupName}"); return ReplaceAction.Replace; } private int mCurrentReplacementNumber; }