Regex 正则表达式获取括号和关键字之间的文本

Regex 正则表达式获取括号和关键字之间的文本,regex,text,keyword,parentheses,Regex,Text,Keyword,Parentheses,我有一个这样的文本模式 (((a) or (b) or (c)) and ((d) or (e)) and ((!f) or (!g))) ((a) or (b) or (c)) ((d) or (e)) ((!f) or (!g)) a,b,c d,e !f,!g 我想这样得到它 (((a) or (b) or (c)) and ((d) or (e)) and ((!f) or (!g))) ((a) or (b) or (c)) ((d) or (e)) ((!f) or (!g)

我有一个这样的文本模式

(((a) or (b) or (c)) and ((d) or (e)) and ((!f) or (!g)))
((a) or (b) or (c))
((d) or (e))
((!f) or (!g))
a,b,c
d,e
!f,!g
我想这样得到它

(((a) or (b) or (c)) and ((d) or (e)) and ((!f) or (!g)))
((a) or (b) or (c))
((d) or (e))
((!f) or (!g))
a,b,c
d,e
!f,!g
然后我想这样把他们分开

(((a) or (b) or (c)) and ((d) or (e)) and ((!f) or (!g)))
((a) or (b) or (c))
((d) or (e))
((!f) or (!g))
a,b,c
d,e
!f,!g
任何帮助都会很棒:)

编辑1:对丢失的零件表示抱歉;使用语言是C#,这就是我得到的

(\([^\(\)]+\))|([^\(\)]+)
与我得到

(a) or (b) or (c) and (d) or (e) and (!f) or (!g)
已经谢谢你了

稍微修改一下代码

string msg= "(((a) or (b) or (c)) and ((d) or (e)) and ((!f) or (!g)))";
var charSetOccurences = new Regex(@"\(((?:[^()]|(?<o>\()|(?<-o>\)))+(?(o)(?!)))\)");
var charSetMatches = charSetOccurences.Matches(msg);
foreach (Match mainMatch in charSetMatches)
{
    var sets = charSetOccurences.Matches(mainMatch.Groups[1].Value);
    foreach (Match match in sets)
    {
        Console.WriteLine(match.Groups[0].Value);
    }
}

如果要删除外部参数,只需更改最里面的一行:

Console.WriteLine(match.Groups[0].Value);

要获得:

(a) or (b) or (c)
(d) or (e)
(!f) or (!g)

我相信你能从这里得到它。

在我工作之后,我想到了这一点

  string msg = "(((a) or (b) or (c)) and ((d) or (e)) and ((!f) or (!g)))";

  Regex regex = null;
  MatchCollection matchCollection = null;

  regex = new Regex(@"(\([^\(\)]+\))|([^\(\)]+)"); // For outer parantheses
  matchCollection = regex.Matches(query);
  foreach (Match match in matchCollection)
  {
    MatchCollection subMatchCollection = Regex.Matches(match.Value, @"(""[^""]+"")|([^\s\(\)]+)"); // For value of inner parantheses
    foreach (Match subMatch in subMatchCollection)
    {
      //with 2 loops i got each elements of this type of string.
    }
  }

谢谢大家!:)

你用的是什么语言?试着自己写一些东西,如果写不出来,具体告诉我们你做了什么,这样我们可以帮助你。你开始,我们帮助。我们不是为你写的。向我们展示您尝试过的实际代码,然后描述发生了什么和什么不正确,然后我们可以从那里帮助您。如果你先自己尝试一下,很有可能你会非常接近答案。很抱歉,遗漏了:)我想如果没有RegEx嵌套元素和RegEx,可能会更容易做到这一点,哦,天哪。谢谢你!我刚刚看到了你的答案,这也行!我也会用我的答案更新我的问题:)