How about this:
string input = "Hello _tmp_ how is _tmp_ this possible _tmp_ in C#...?"; string[] array = { "value1", "value2", "value3" }; Regex rx = new Regex(@"\b_tmp_\b"); if (rx.Matches(input).Count <= array.Length) { int index = 0; string result = rx.Replace(input, m => array[index++]); Console.WriteLine(result); }
You need to make sure that the number of matches found never exceeds the length of the array, as shown above.
EDIT: in response to a comment, this can easily work with C # 2.0, replacing the lambda as follows:
string result = rx.Replace(input, delegate(Match m) { return array[index++]; });
Ahmad mageed
source share