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

本文关键字:paraenthisis 递归 嵌套 正则表达式 | 更新日期: 2023-09-27 17:57:20

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

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

结果字符串数组应为 -

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

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

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

感谢您的帮助。经过一番思考,我能够找到简单的解决方案。

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('|')));
            }