I am trying to do a simple replacement utilizing the static RegEx.Replace method and it fails if inside the replacement string I have “$0.00” or some sort of derivative of this.
Here is the code:
void Main()
{
try
{
string inputString = "[BEGIN-LOOP:DETAILS]this is what I want to replace[END-LOOP:DETAILS]";
string replacementString = "some text $0.00";
inputString = Regex.Replace(inputString, @"(\[BEGIN-LOOP:DETAILS\])(.*?)(\[END-LOOP:DETAILS\])", replacementString, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Console.WriteLine(inputString);
}
catch (Exception ex)
{
throw;
}
}
Output of the failed RegEx is:
some text [BEGIN-LOOP:DETAILS]this is what I want to replace[END-LOOP:DETAILS].00
It should be
some text $0.00
You need to escape the dollar as shown on the MSDN page on Substitutions.
So you want:
As an aside, please don’t use try/catch blocks like that… it clutters up your code for no benefit.