using System; public class Test { enum State { WhiteSpace, Block, String }; static string Parse(string s) { State state = State.WhiteSpace; int? blockStart = null; for (int p = 0; ; ) { switch (state) { case State.WhiteSpace: if (s[p] == '{') { p++; state = State.Block; blockStart = p; } else if (s[p] == ' ') { p++; } else { throw new System.ArgumentException(); } break; case State.Block: if (s[p] == '}') { return s.Substring((int) blockStart, p - (int) blockStart); } else if (s[p] == '"') { p++; state = State.String; } else { p++; } break; case State.String: if (s[p] == '"') { p++; state = State.Block; } else { p++; } break; } } throw new System.ArgumentException(); } public static void Main() { Console.Out.WriteLine(Parse("{ --isso é um bloco;\n echo \"Aqui tem um } no meio\";}")); } }