public string FormatVars(string text) { Regex r = new Regex("~(.+?)~"); MatchCollection mC = r.Matches(text); for (int i = 0; i < mC.Count; i++) text = text.Replace(mC[i].ToString(), string.Format("{{0}}",i)); return text; }