Note that there are some explanatory texts on larger screens.

plurals
  1. POHow Can I Extract and Split These Values?
    primarykey
    data
    text
    <p>Here is an example of a string that I am working with</p> <pre><code>{Hi|Hello|Holla} {James{ey|o|ing}|Bob{bie|bey}} </code></pre> <p>I need a regular expression to extract the values between the {}'s example:</p> <pre><code>Hi|Hello|Holla James{ey|o|ing} Bob{bie|bey} </code></pre> <p>The original string is called Spintax. My program will select a random value enclosed within each {} block. The nested {} blocks can go pretty deep.</p> <p>The regular expression needs to extract the value between the {} ignoring any nested {} blocks. And then, split the value by the pipe (|) again ignoring any nested {} blocks so that the pipes within nested {} blocks are not touched.</p> <p>Does that make sense?</p> <p>I did implement partial solution using String methods, but when splitting by pipes it splits the pipes within the nested {} too, which is to be expected, but I can't figure out a way to ignore the nested {}</p> <pre><code>public String spintaxParse(String s) { // TODO: Implement logic to check for {} within String. if (s.Contains('{')) { int firstOccuranceOfOpenBrace = s.IndexOf('{'); while (s[firstOccuranceOfOpenBrace + 1].Equals('{')) firstOccuranceOfOpenBrace++; int firstOccuranceOfClosingBrace = s.Substring(firstOccuranceOfOpenBrace).IndexOf('}'); String spintaxBlock = s.Substring(firstOccuranceOfOpenBrace, firstOccuranceOfClosingBrace + 1); String[] items = spintaxBlock.Substring(1, spintaxBlock.Length - 2).Split('|'); Random rand = new Random(); s = s.Replace(spintaxBlock, items[rand.Next(items.Length)]); return spintaxParse(s); } else { return s; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload