-5

我需要编写一个函数,该函数将在字符串限制中找到[insert - l] some text [/insert]并在每个单词中放入一个限制中指定的字符(例如 char l )我认为我需要在 [insert - l] [/insert] 之间找到所有拆分它排列并将一个字符放入随机位置的每个单词中,但我的knowlogde不足以编写它。请帮助我=\

例子:

Musgrave was [insert - l] selected as a scientist-astronaut [/insert] by NASA in August 1967. 

功能后我必须得到:

Musgrave was sellected als a scilentist-astrlonaut by NASA in August 1967.
4

1 回答 1

2

只因为我是个贪吃惩罚的人。这是我对你要求的猜测。

var random = new Random();
string bigBlockOfText = @"blah blah [insert - l] some text [/insert] blah blah blah [insert - q] different text[/insert].";
var reg = new Regex(@"\[insert \- (?<char>.)\](?<text>.+?)\[/insert\]");
var matches = reg.Matches(bigBlockOfText);
for (int i = matches.Count - 1; i >= 0; i--)
{
    var match = matches[i];
    bigBlockOfText = bigBlockOfText.Remove(match.Index, match.Length);
    var replace = match.Groups["text"].Value;
    var insert = match.Groups["char"].Value;
    replace = replace.Insert(random.Next(replace.Length), insert);
    bigBlockOfText = bigBlockOfText.Insert(match.Index, replace);
}
于 2012-12-20T20:36:34.850 回答