2015-06-20 101 views
-1

需要帮助才能找到正则表达式。C#正则表达式嵌套Paraenthisis递归

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

结果字符串数组应该是 -

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

除非两个或多个字符串被拆分|,我需要他们的一边到另一边。

+1

这不是正则表达式服务,你必须自己显示一些努力。你不是在这里寻求帮助,而是为你做功课的人。 – CSharpie

+0

在C#中执行它......这可能在正则表达式中可行,但它会是地狱。 – xanatos

+0

感谢评论xanatos,我是C#的新手,并试图创建一个递归函数来将它们除以{|}。任何参考将有所帮助。 –

回答

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