6

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 replacement-string which results in problems.

How am I able to do that directly with the Replace-method? I only found a workaround by adding some temporary garbage that I instantly remove again.

See code for the problem:

      // We want to add a dollar sign before a number and use named groups for capturing;
      // varying parts of the strings are in brackets []
      // [somebody] has [some-dollar-amount] in his [something]

      string joeHas = "Joe has 500 in his wallet.";
      string jackHas = "Jack has 500 in his pocket.";
      string jimHas = "Jim has 740 in his bag.";
      string jasonHas = "Jason has 900 in his car.";

      Regex dollarInsertion = new Regex(@"(?<start>^.*? has )(?<end>\d+ in his .*?$)", RegexOptions.Multiline);

      Console.WriteLine(joeHas);
      Console.WriteLine(jackHas);
      Console.WriteLine(jimHas); 
      Console.WriteLine(jasonHas);
      Console.WriteLine("--------------------------");

      joeHas = dollarInsertion.Replace(joeHas, @"${start}$${end}");
      jackHas = dollarInsertion.Replace(jackHas, @"${start}$-${end}");          
      jimHas = dollarInsertion.Replace(jimHas, @"${start}\$${end}");
      jasonHas = dollarInsertion.Replace(jasonHas, @"${start}$kkkkkk----kkkk${end}").Replace("kkkkkk----kkkk", "");

      Console.WriteLine(joeHas);
      Console.WriteLine(jackHas);
      Console.WriteLine(jimHas);
      Console.WriteLine(jasonHas);




Output:
Joe has 500 in his wallet.
Jack has 500 in his pocket.
Jim has 740 in his bag.
Jason has 900 in his car.
--------------------------
Joe has ${end}
Jack has $-500 in his pocket.
Jim has \${end}
Jason has $900 in his car.

2 Answers 2

13

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);
Sign up to request clarification or add additional context in comments.

Comments

2

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);

3 Comments

Presumably the OP needs to extract that information from the string. It is not readily available.
Just like Ahmad said. The example I gave was just to picture the replacement problem and the real strings and the real use of this is not for simple sentences.
That's fair. If your just replacing tokens, though, regex is overkill, and slower than alternatives. (RegEx isn't slow, but it can't match the speed/simplicity of a normal .Replace())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.