0

我得到了这个用于旋转文本的类。

    public class Spinner
    {
        private static Random rnd = new Random();
        public static string Spin(string str)
        {
            string regex = @"\{(.*?)\}";
            return Regex.Replace(str, regex, new MatchEvaluator(WordScrambler));
        }
        public static string WordScrambler(Match match)
        {
            string[] items = match.Value.Substring(1, match.Value.Length - 2).Split('|');
            return items[rnd.Next(items.Length)];
        }
    }

但我需要它能够旋转多 spintax 文本。

例如 {1|2} - {3|4}

返回:2 - 4 所以它有效。

但是:{{1|2}|{3|4}} - {{5|6}|{7|8}}

返回:2|4} - {5|7}

所以如果spintax里面有spintax就不行了。

有什么帮助吗?:)

4

1 回答 1

1

正则表达式不擅长处理嵌套结构,这意味着您可能应该尝试不同的方法。

在您的示例中,{{1|2}|{3|4}} - {{5|6}|{7|8}}与 相同{1|2|3|4} - {5|6|7|8},因此您可能不需要嵌套的 spintax。

于 2014-01-17T13:24:15.333 回答