C# dollar problem with regex-replace

后端 未结 2 866
执念已碎
执念已碎 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);
    

提交回复
热议问题