Here is an example of a string that I am working with
{Hi|Hello|Holla} {James{ey|o|ing}|Bob{bie|bey}}
I need a regular expression to extract the values between the {}'s example:
Hi|Hello|Holla
James{ey|o|ing}
Bob{bie|bey}
The original string is called Spintax. My program will select a random value enclosed within each {} block. The nested {} blocks can go pretty deep.
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.
Does that make sense?
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 {}
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;
}
}
Hi
Hello
Holla
James{ey|o|ing}
Bob{bie|bey}
is a seperator – ojlovecd Nov 04 '11 at 06:38