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

Free Support Forum - aspose.com

Replacing text with Regex throws exception at second replacement

I’m replacing text tags to inside a doc with the following structure: ${TagName}

So, I created a regex using this code:
            string prefix = Regex.Escape(config.TagPrefix);
string sufix = !String.IsNullOrWhiteSpace(config.TagSufix) ? Regex.Escape(config.TagSufix) : “”;
        <span style="color: blue;">string</span> pattern = prefix + <span style="color: rgb(163, 21, 21);">@"\w+"</span> + sufix;
        <span style="color: blue;">var</span> regex = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Regex</span>(pattern);</pre><pre style="background: white;"><font face="Times New Roman"><span style="white-space: normal;">Although at the second line, on suffix, I have a workaround when the configurable suffix is null, both the prefix and suffix are respectively "${" and "}". That way forming tags with the struct I showed before.</span></font></pre><pre style="background: white;"><font face="Times New Roman"><span style="white-space: normal;">But, the replacement throws the following exception, apparently randomly, sometimes at the second replacement, others at third, but never goes farther than this, replacing the first occurrence with success but rarely go beyond the second or third replacement.</span></font></pre><pre style="background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><pre style="background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><font face="Courier New" size="2">System.ArgumentOutOfRangeException : StartIndex cannot be less than zero.

Parameter name: startIndex
at System.String.Remove(Int32 startIndex, Int32 count)
at .(ReplacingArgs )
at .()
at Aspose.Words.Range.Replace(Regex pattern, IReplacingCallback handler, Boolean isForward)

… call stack from my code omitted.
When I change the tag structure to something more simple (without the sufix and prefix) and with a fixed tag, the replacement works fine.
What am I doing wrong ?

In the meantime, I checked in more details the code and I found the parameter “isForward” with 'true’in the overload:

public int Replace(Regex pattern, IReplacingCallback handler, bool isForward)
When I changed it to false, as shown in sample code, it worked. 
Now my question is: What is this paramater for ? What the exception is thrown when I use ‘true’ ?

Hi Luciano,


Thanks for your inquiry. You’re right. This issue does not occur if you use backward evaluation instead of forwards in your code. This is just a matter of changing the third parameter of the replace method to false. Generally it is recommend if you are modifying the document in a custom replacement evaluator then you should use backward replacement.

Best regards,