split a string only on spaces that are outside curly braces

前端 未结 3 1140
耶瑟儿~
耶瑟儿~ 2021-01-16 17:11

I am new to regex and I need some help. I read some topics similar to this issue, but I could not figure out how to resolve it.

I need to split a string on every bla

3条回答
  •  失恋的感觉
    2021-01-16 17:25

    Here is exactly what you want...

    string Source = "{ TEST test } test { test test} {test test } { 123 } test test";
    List Result = new List();
    StringBuilder Temp = new StringBuilder();
    bool inBracket = false;
    foreach (char c in Source)
    {
        switch (c)
        {
            case (char)32:       //Space
                if (!inBracket)
                {
                    Result.Add(Temp.ToString());
                    Temp = new StringBuilder();
                }
                break;
            case (char)123:     //{
                inBracket = true;
                break;
            case (char)125:      //}
                inBracket = false;
                break;
        }
        Temp.Append(c);
    }
    if (Temp.Length > 0) Result.Add(Temp.ToString());
    

提交回复
热议问题