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.

3
  • 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: stackoverflow.com/questions/31648824/… Commented Sep 2, 2016 at 7: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. Commented Sep 2, 2016 at 7:56
  • How should something like string.Format($"alpha: {alphaID}, {0}, betaValue: {1} {beta.Value}", varOne, varTwo) be handled? Commented Sep 2, 2016 at 8:14

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

2 Comments

I don't think this malformed PCRE regex can help much in C#.
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.
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

Comments

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.