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 ?