Do not worry about nesting, just do iteratively as follows:
Find the first sequence in the line with {...} without any other curly braces inside. For your case, this is {Hello|Hi} . If this template is no longer available, go to step 3.
Take all the possibilities and choose random, replacing the bracket section with its value. Then return to step 1.
There is your modified line.
Let's say you have a slightly faulty random number generator that always returns zero. Then your line change history will look like this:
a / {Hello | Hi} {World | People}! {C {# | ++ |} | Java} is an {awesome | amazing} language.
b / Hello {World | People}! {C {# | ++ |} | Java} is an {awesome | amazing} language.
c / Hello World! {C {# | ++ |} | Java} is an {awesome | amazing} language.
d / Hello World! {C # | Java} is an {awesome | amazing} language.
e / Hello World! C # is an {awesome | amazing} language.
f / Hello World! C # is an awesome language.
Please note in particular the transition from (c) to (d). Since we are looking for the first part of a brace that does not have brackets inside, we do {#|++|} to {C{#|++|}|Java} .
All you need to add is the possibility that you may have { , } or | in your actual text - you will need to somehow avoid them in order to protect them from your modification mechanism.
Here is a small C # program that shows this in action. This is probably not so impressive written, given my relative inexperience with the language, but it illustrates the process.
using System; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static string spintax(Random rnd, string str) {
The output in one example is
Hello World! C # is an awesome language.
source share