1

Me and my colleague have different versions of VisualStudio. He used interpolated string and I couldn't build the solution and had to convert them all to string.Format.

Now I thought it might be good exercise for regex.

So how to convert this:

$"alpha: {alphaID}, betaValue: {beta.Value}"

To this:

string.Format("alpha: {0}, betaValue: {1}", alphaID, beta.Value)

Now the number of variables can vary (let's say 1 - 20, but should be generic)

I came up with this regex, to match the first variable

\$.*?{(\w+)}

but I couldn't figure out how to repeat the part after dollar sign, so I can repeat the result.

Zikato
  • 536
  • 9
  • 18
  • 3
    Do you know you also need to account for `{{` and `}}` literal braces? That is not the best job for a regex to parse code. Related: http://stackoverflow.com/questions/31648824/how-to-refactor-c-sharp-interpolated-strings-to-string-format-automatically – Wiktor Stribiżew Sep 02 '16 at 07:15
  • Also tell him to configure his version of VS such that the code does not make use of C#6 features. That is possible. – Bernhard Hiller Sep 02 '16 at 07:56
  • How should something like `string.Format($"alpha: {alphaID}, {0}, betaValue: {1} {beta.Value}", varOne, varTwo)` be handled? – AdrianHHH Sep 02 '16 at 08:14

4 Answers4

3

Regex.Replace has an overload which takes a function, called the MatchEvaluator. You might use it like this;

 var paramNumber = 0;     
 var idNames = new List<string>();
 myCSharpString = Regex.Replace(myCSharpString, match => {
    // remember the id inside brackets;
    idNames.Add(match.ToString());

    // return "0", then "1", etc.
    return (paramNumber++).ToString();
 });

At the end of this process, your string like "this is {foo} not {bar}" will have been replaced to "this is {0} not {1}" and you will have a list containing { "foo" , "bar" } which you can use to assemble the parameter list.

Steve Cooper
  • 20,542
  • 15
  • 71
  • 88
2

You can use C#6 features in older version of visual studio using the C# 6 nuget package

Essentially, just use

Install-Package Microsoft.Net.Compilers

On all the projects.

Community
  • 1
  • 1
Steve Cooper
  • 20,542
  • 15
  • 71
  • 88
0

I dont have experience in c# but this may help you:

\{(.+)}\gU

Explanation

{ matches the character { literally

. matches any character (except newline)

Quantifier: + Between one and unlimited times, as few times as possible, expanding as needed [lazy]

} matches the character } literally

g modifier: global. All matches (don't return on first match)

U modifier: Ungreedy

Crunch Much
  • 1,537
  • 1
  • 11
  • 14
  • I don't think this malformed PCRE regex can help much in C#. – Wiktor Stribiżew Sep 02 '16 at 07:50
  • It doesn't matter, I can just enter the code in notepad++ or other regex substitute tool and run it. I will get rid of \gU and insert lazy operator (?) after plus sign. – Zikato Sep 02 '16 at 07:52
0

Here are few options that do not involve regex:

  1. You can use ReSharper. It has such conversions built in.
  2. Write your custom code fix with Roslyn if you don't want to pay for ReSharper. Here is an example that does from string.format to interpolated string, you just have to reverse it.

  3. Do it manually

  4. Ask your colleague to do it for you if he can.
  5. Update VS
Nikola Sivkov
  • 2,812
  • 3
  • 37
  • 63