C#正则表达式嵌套的Paraenthisis递归

时间:2015-06-20 08:29:28

标签: c# regex recursion nested

需要帮助才能找到正则表达式。

Text = @"{'the quick' | 'the lazy'}{{{'BEFORE'} 'fox' } | {{'BEFORE'} 'lion'}}"

结果字符串数组应为 -

[0] = 'the quick' | 'the lazy',
[1] = BEFORE/1 fox | BEFORE/2 lion

除非两个或多个字符串被|拆分,否则我需要它们并排。

1 个答案:

答案 0 :(得分:0)

感谢您的帮助。在思考了一下后,我找到了简单的解决方案。

string sampleText = "{'what is' | 'when is'}{BEFORE/2 }{doing | 'to do'}{ NEAR/3 }{'to ensure our' | 'to ensure that' | 'to make sure our' | 'to make sure that our' | 'so our' | 'so that our'}{ BEFORE/4 }{doesn*t | 'does not' | 'will not' | won*t}";
            List<List<string>> list = new List<List<string>>();
            Match mt = Regex.Match(sampleText, @"\}([^{]*)\{");
            string value = sampleText.Replace(mt.Value, "},{");
            string[] newArray = value.Split(",".ToCharArray());
            foreach (string st in newArray)
            {
                list.Add(new List<string>(st.Replace("{", "").Replace("}", "").Split('|')));
            }