This will take an input string with words inside brackets and replace them with a random word from the list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Text.RegularExpressions; namespace RandomMacroExample { class Program { static Random random = new Random((int)DateTime.Now.Ticks); static string RandomMacro(string input) { Regex rx = new Regex(@"{(.*?)}"); string result = rx.Replace(input, new MatchEvaluator((Match m) => { string[] x = m.Groups[1].ToString().Split(new char[] { '|' }); return x[random.Next(0, x.Length)]; })); return result; } static void Main(string[] args) { Console.WriteLine(RandomMacro("This stuff is totally {cool|lame|stupid|awesome}. Lets go to the {mall|skate shop|gas station}")); Console.WriteLine(RandomMacro("This stuff is totally {cool|lame|stupid|awesome}. Lets go to the {mall|skate shop|gas station}")); Console.WriteLine(RandomMacro("This stuff is totally {cool|lame|stupid|awesome}. Lets go to the {mall|skate shop|gas station}")); Console.WriteLine(RandomMacro("This stuff is totally {cool|lame|stupid|awesome}. Lets go to the {mall|skate shop|gas station}")); Console.WriteLine(RandomMacro("This stuff is totally {cool|lame|stupid|awesome}. Lets go to the {mall|skate shop|gas station}")); Console.ReadLine(); } } } |