无法用包含$ 0的动态字符串替换文本 [英] Trouble replacing text with a dynamic string that contains $0

查看:50
本文介绍了无法用包含$ 0的动态字符串替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Regex 替换模板中的所有字符串.一切正常,直到要替换的值是 $ 0.00 .我似乎无法正确替换 $ 0 作为替换文本.我得到的输出是"Project Cost:[[ProjectCost]].00" .知道为什么吗?

I am using Regex to replace all the strings in a template. Everything works fine until there is a value I want to replace, which is $0.00. I can't seem to properly replace the $0 as replacement text. The output I am getting is "Project Cost: [[ProjectCost]].00". Any idea why?

这是带有一些简化变量的代码示例.

Here is an example of the code with some simplified variables.

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

using System.Security;
using System.Text.RegularExpressions;


namespace Export.Services
{
    public class CommonExportService
    {

        private Dictionary<string, string> _formTokens;


        public CommonExportService() {
         _formTokens =     {{"EstimatedOneTimeProjectCost", "0.00"}};
        }


        private string GetReplacementText(string replacementText)
        {
            replacementText = "Project Cost: [[EstimatedOneTimeProjectCost]]";
            //replacement text = "Project Cost: [[ProjectCost]]"
            foreach (var token in _formTokens)
            {
                var val = token.Value;
                var key = token.Key;

                //work around for now
                //if (val.Equals("$0.00")) {
                //    val = "0.00";
                //}

                var reg = new Regex(Regex.Escape("[[" + key + "]]"));


                if (reg.IsMatch(replacementText))                        
                    replacementText = reg.Replace(replacementText, SecurityElement.Escape(val ?? string.Empty));
                else {

                }
            }

            return replacementText;

            //$0.00 does not replace,  something is happening with the $0 before the decimal  
            //the output becomes Project Cost: [[EstimatedOneTimeProjectCost]].00


            //The output is correct for these
            //0.00 replaces correctly
            //$.00 replaces correctly
            //0 replaces correctly
            //00 replaces correctly
            //$ replaces correctly

        }
    }
}

推荐答案

由于替换字符串是动态生成的,因此您需要注意其中的 $ char.当 $ 后跟 0 时, $ 0 是对整个匹配项的反向引用,因此作为替换结果插入了整个匹配项.

Since your replacement string is built dynamically, you need to take care of the $ char in it. When $ is followed with 0, the $0 is a backreference to the whole match, so the whole match is inserted as a result of replacement.

您只需要在文字字符串模式内对 $ 进行美元转义:

You just need to dollar-escape the $ inside a literal string pattern:

return replacementText.replace("$", "$$");

然后,您的替换模式将包含 $$ 0 ,并将其转换"为原义的 $ 0 字符串.

Then, your replacement pattern will contain $$0, and that will "translate" into a literal $0 string.

这篇关于无法用包含$ 0的动态字符串替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆