用动态数字替换文本中的单词/值

obd*_*dgy 4 c# replace

对于前者 我有字符串

string text = @"Today is {Rand_num 15-22} day. {Rand_num 11-55} number of our trip.";
Run Code Online (Sandbox Code Playgroud)

我需要用兰特数替换每个Rand_num构造(在规定的数字15-22或11-55之间)

试过smth但不知道接下来该做什么

string text = @"Today is {Rand_num 15-22} day. {Rand_num 11-55} number of our trip.";
if (text.Contains("Rand_num"))
{              
    string groups1 = Regex.Match(text, @"{Rand_num (.+?)-(.+?)}").Groups[1].Value;
    string groups2 = Regex.Match(text, @"{Rand_num (.+?)-(.+?)}").Groups[2].Value;               
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 8

怎么样:

Random rand = new Random();
text = Regex.Replace(text, @"{Rand_num (.+?)-(.+?)}", match => {
    int from = int.Parse(match.Groups[1].Value),
        to = int.Parse(match.Groups[2].Value);
    // note end is exclusive
    return rand.Next(from, to + 1).ToString();
});
Run Code Online (Sandbox Code Playgroud)

显然输出会有所不同,但我得到:

今天是21天.25号旅行.