0

I got this class that I'm using for spin text.

    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)];
        }
    }

But I need it to be able to spin multi spintax text.

As example {1|2} - {3|4}

Returns: 2 - 4 So it works.

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

Returns: 2|4} - {5|7}

So it doesn't work if there is spintax inside spintax.

Any help? :)

plexcell
  • 1,577
  • 2
  • 11
  • 27
  • possible duplicate of [Spintax C# ... How can I handle this?](http://stackoverflow.com/questions/8004465/spintax-c-sharp-how-can-i-handle-this) – Thomas Weller Jan 17 '14 at 13:47

1 Answers1

1

Regular expressions are not good in dealing with nested structures, which means you should probably try a different approach.

In your example, {{1|2}|{3|4}} - {{5|6}|{7|8}} is the same as {1|2|3|4} - {5|6|7|8}, so maybe you don't need nested spintax.

Community
  • 1
  • 1
Thomas Weller
  • 43,638
  • 16
  • 101
  • 185