C# dollar problem with regex-replace

后端 未结 2 867
执念已碎
执念已碎 2020-12-19 08:39

I want to insert a dollar sign at a specific position between two named capturing groups. The problem is that this means two immediately following dollar-signs in the replac

相关标签:
2条回答
  • 2020-12-19 08:55

    Use this replacement pattern: "${start}$$${end}"

    The double $$ escapes the $ so that it is treated as a literal character. The third $ is really part of the named group ${end}. You can read about this on the MSDN Substitutions page.

    I would stick with the above approach. Alternately you can use the Replace overload that accepts a MatchEvaluator and concatenate what you need, similar to the following:

    jackHas = dollarInsertion.Replace(jackHas,
                  m => m.Groups["start"].Value + "$" + m.Groups["end"].Value);
    
    0 讨论(0)
  • 2020-12-19 09:10

    Why are you using regex for this in the first place?

    string name = "Joe";
    int amount = 500;
    string place = "car";
    
    string output = string.Format("{0} has ${1} in his {2}",name,amount,place);
    
    0 讨论(0)
提交回复
热议问题